Adding multiple rows of a cell array
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chris Dan
am 12 Jul. 2020
Kommentiert: Star Strider
am 12 Jul. 2020
Hello, I have a 5x1 cell array, in which each cell is a 6x64 double.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/330782/image.jpeg)
For each cell, I want to add the rows in this style,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/330785/image.jpeg)
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?
0 Kommentare
Akzeptierte Antwort
Star Strider
am 12 Jul. 2020
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 Kommentare
Weitere Antworten (1)
madhan ravi
am 12 Jul. 2020
Bearbeitet: madhan ravi
am 12 Jul. 2020
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
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!