why my loop keeps only the last run?

1 Ansicht (letzte 30 Tage)
Ivan Mich
Ivan Mich am 30 Jan. 2023
Beantwortet: Image Analyst am 30 Jan. 2023
I am creating a zeros cell with dimensions 2x4 in order to work in a loop. the problem is that my script do not keep all the runs of the loop, but only the last one. As a result I have a cell array 1x4 and not 2x4 as I wanted.
I have tried:
C=zeros(2,4);
C=num2cell(C);
for z=1:2
C=[A(z) B(z) D(z) E(z)]
end
%A,B,D,E are variables
Could you please help me?

Akzeptierte Antwort

Image Analyst
Image Analyst am 30 Jan. 2023
You're not indexing C so you're just overwriting it every time. Fix:
C=zeros(2,4);
for z=1:2
C(z, :) =[A(z) B(z) D(z) E(z)];
end

Weitere Antworten (0)

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!

Translated by