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

조회 수: 1 (최근 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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by