How to sum a single column of a cell whose content are vectors

2 ビュー (過去 30 日間)
Hudson Romualdo
Hudson Romualdo 2022 年 10 月 11 日
回答済み: Andres 2022 年 10 月 11 日
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.

採用された回答

Andres
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)))

その他の回答 (1 件)

David Hill
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
s = 129.3577
  3 件のコメント
David Hill
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)))
s = 135.0550
Hudson Romualdo
Hudson Romualdo 2022 年 10 月 11 日
Nice! =)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by