Store each value generated in a for loop

3 Ansichten (letzte 30 Tage)
Ross Thompson
Ross Thompson am 9 Mär. 2022
Kommentiert: David Hill am 11 Mär. 2022
I have a bit of code which generates a value P and updates it every iteration. How can i store each of these values generated and then plot them on a graph?

Antworten (1)

David Hill
David Hill am 9 Mär. 2022
for k=1:100
p(k)=%your update for p
end
plot(p);
  2 Kommentare
Ross Thompson
Ross Thompson am 10 Mär. 2022
I keep getting the error 'Unable to perform assignment because the left and right sides have a different number of elements.'
P=zeros(1000,1);
for n = 1:1000
P(n) = nonzeros(Q_new.*C);
end
Any ideas why this might be? Q and C are both 10x100 matricies which also update every iteration
David Hill
David Hill am 11 Mär. 2022
Use a cell array. Your sizes each iteration might be different based on the number of nonzeros.
P=cell(1000,1);
for n = 1:1000
P{n} = nonzeros(Q_new.*C);
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by