Adding multiple rows of a cell array

조회 수: 13 (최근 30일)
Chris Dan
Chris Dan 2020년 7월 12일
댓글: Star Strider 2020년 7월 12일
Hello, I have a 5x1 cell array, in which each cell is a 6x64 double.
For each cell, I want to add the rows in this style,
1st, 3rd and 5th rows addded together to make one row
2nd 4th and 6th row added together to make one row.
Can it be done without loop?
I am attaching the file . I don't have any code written yet, because I dont know how to do it..
Does anybody know?

채택된 답변

Star Strider
Star Strider 2020년 7월 12일
Try this:
D = load('NumericalFourierForce.mat');
Numerical_FourierForce = D.Numerical_FourierForce;
Out = cellfun(@(x){sum(x(1:2:end,:)); sum(x(2:2:end,:))}, Numerical_FourierForce, 'Uni',0);
Out{1}{1,:} % Check Output
Out{1}(2,:) % Check Output
Out{5}{1,:} % Check Output
Out{5}(2,:) % Check Output
Notice that there are no explicit loops. (There are obviously loops within cellfun.)
  댓글 수: 2
Chris Dan
Chris Dan 2020년 7월 12일
Thanks :)
Star Strider
Star Strider 2020년 7월 12일
As always, my pleasure!

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

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 7월 12일
편집: madhan ravi 2020년 7월 12일
Edit: cellfun() is a loop in disguise by the way.
for k = 1:numel(cell_array)
cell_array{k}(end+1,:) = sum(cell_array{k}(1:2:5,:));
cell_array{k}(end+1,:) = sum(cell_array{k}(2:2:6,:));
end

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by