如何将'double'转化为'cell array'?
How to transform 'double' into 'cell array'?
我的代码:
B = zeros(height(A),1);
col_names = A.Properties.VariableNames; % Replicate header names
for k = 1:height(A)
% the following 'cellfun' compares each column to the values in A.L{k},
% and returns a cell array of the result for each of them, then
% 'cell2mat' converts it to logical array, and 'any' combines the
% results for all elements in A.L{k} to one logical vector:
C = any(cell2mat(...
cellfun(@(x) strcmp(col_names,x),A.L{k},...
'UniformOutput', false).'),1);
% then a logical indexing is used to define the columns for summation:
B(k) = sum(A{k,C});
end
生成以下错误消息。
Error using cellfun
Input #2 expected to be a cell array, was double instead.
如何解决这个错误?
这是 table 'A' 的样子:
A.L{1,1} 包含:
C = any(cell2mat(...
cellfun(@(x) strcmp(col_names,x),A.L{k},...
'UniformOutput', false).'),1);
此处A.L{k}
获取位于A.L
第k个单元格的内容。使用 A.L(k)
你会得到位于 A.L
:
的单元格本身
tmp = A.L(k);
C = any(cell2mat(...
cellfun(@(x) strcmp(col_names,x),tmp{1},...
'UniformOutput', false).'),1);
有点老套,因为您首先需要获取位于 A.L(k)
的单元格,然后 然后 需要该单元格的内容,因此您需要一个临时变量.
我不完全确定这里发生了什么,但这是一个我认为与您想要实现的相似的虚构示例。
%% Setup - fabricate some data
colNames = {'xx', 'yy', 'zz', 'qq'};
h = 20;
% It looks like 'L' contains something related to the column names
% so I'm going to build something like that.
L = repmat(colNames, h, 1);
% Empty some rows out completely
L(rand(h,1) > 0.7, :) = {''};
% Empty some other cells out at random
L(rand(numel(L), 1) > 0.8) = {''};
A = table(L, rand(h,1), rand(h, 1), rand(h, 1), rand(h, 1), ...
'VariableNames', ['L', colNames]);
%% Attempt to process each row
varNames = A.Properties.VariableNames;
B = zeros(height(A), 1);
for k = 1:height(A)
% I think this is what's required - work out which columns are
% named in "A.L(k,:)". This can be done simply by using ISMEMBER
% on the row of A.L.
C = ismember(varNames, A.L(k,:));
B(k) = sum(A{k, C});
end
如果我在这里完全偏离轨道,那么也许你可以给我们一个可执行的例子。
我的代码:
B = zeros(height(A),1);
col_names = A.Properties.VariableNames; % Replicate header names
for k = 1:height(A)
% the following 'cellfun' compares each column to the values in A.L{k},
% and returns a cell array of the result for each of them, then
% 'cell2mat' converts it to logical array, and 'any' combines the
% results for all elements in A.L{k} to one logical vector:
C = any(cell2mat(...
cellfun(@(x) strcmp(col_names,x),A.L{k},...
'UniformOutput', false).'),1);
% then a logical indexing is used to define the columns for summation:
B(k) = sum(A{k,C});
end
生成以下错误消息。
Error using cellfun
Input #2 expected to be a cell array, was double instead.
如何解决这个错误?
这是 table 'A' 的样子:
A.L{1,1} 包含:
C = any(cell2mat(...
cellfun(@(x) strcmp(col_names,x),A.L{k},...
'UniformOutput', false).'),1);
此处A.L{k}
获取位于A.L
第k个单元格的内容。使用 A.L(k)
你会得到位于 A.L
:
tmp = A.L(k);
C = any(cell2mat(...
cellfun(@(x) strcmp(col_names,x),tmp{1},...
'UniformOutput', false).'),1);
有点老套,因为您首先需要获取位于 A.L(k)
的单元格,然后 然后 需要该单元格的内容,因此您需要一个临时变量.
我不完全确定这里发生了什么,但这是一个我认为与您想要实现的相似的虚构示例。
%% Setup - fabricate some data
colNames = {'xx', 'yy', 'zz', 'qq'};
h = 20;
% It looks like 'L' contains something related to the column names
% so I'm going to build something like that.
L = repmat(colNames, h, 1);
% Empty some rows out completely
L(rand(h,1) > 0.7, :) = {''};
% Empty some other cells out at random
L(rand(numel(L), 1) > 0.8) = {''};
A = table(L, rand(h,1), rand(h, 1), rand(h, 1), rand(h, 1), ...
'VariableNames', ['L', colNames]);
%% Attempt to process each row
varNames = A.Properties.VariableNames;
B = zeros(height(A), 1);
for k = 1:height(A)
% I think this is what's required - work out which columns are
% named in "A.L(k,:)". This can be done simply by using ISMEMBER
% on the row of A.L.
C = ismember(varNames, A.L(k,:));
B(k) = sum(A{k, C});
end
如果我在这里完全偏离轨道,那么也许你可以给我们一个可执行的例子。