How to sum a single column of a cell whose content are vectors
Show older comments
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.
Accepted Answer
More Answers (1)
David Hill
on 11 Oct 2022
Edited: David Hill
on 11 Oct 2022
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 Comments
Hudson Romualdo
on 11 Oct 2022
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)))
Hudson Romualdo
on 11 Oct 2022
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!