How to sum a single column of a cell whose content are vectors
2 次查看(过去 30 天)
显示 更早的评论
How to sum a single column of a cell whose content is a vector?
data = cell(20,2);
for i=1:20
data{i,1} = i;
data{i,2} = rand(1,13);
end
My goal is to sum all values of column 2 (that are vectors) in one scallar.
0 个评论
采纳的回答
Andres
2022-10-11
Using cellfun is fine, but use it with the sum function.
data = cell(20,2);
for i=1:20
data{i,1} = i;
data{i,2} = rand(1,13);
end
value = sum(cellfun(@sum,data(:,2)))
0 个评论
更多回答(1 个)
David Hill
2022-10-11
编辑:David Hill
2022-10-11
Simple loop.
data = cell(20,2);
for i=1:20
data{i,1} = i;
data{i,2} = rand(1,13);
end
s=0;
for k=1:size(data,1)
s=s+sum(data{k,2});
end
s
3 个评论
David Hill
2022-10-11
data = cell(20,2);
for i=1:20
data{i,1} = i;
data{i,2} = rand(1,13);
end
s=sum(arrayfun(@(x)sum(data{x,2}),1:size(data,1)))
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!