A simple for loop question

5 visualizzazioni (ultimi 30 giorni)
Ulrich Achas
Ulrich Achas il 9 Mag 2021
Commentato: Ulrich Achas il 9 Mag 2021
isb=6;
for i=1:3
nta=(1+isb).^2/isb
isb=nta;
end
Hello everyone. I have just started to use MATLAB and I wrote the code above. My question is: When I ran this code, MATLAB gave me only last value of nta. But I need all nta values as an matrix. How can I write it? Thanks.
  2 Commenti
Jonas
Jonas il 9 Mag 2021
use nta(i) in both cases
Ulrich Achas
Ulrich Achas il 9 Mag 2021
thank you.

Accedi per commentare.

Risposta accettata

Mazdack Ameri
Mazdack Ameri il 9 Mag 2021
Modificato: Mazdack Ameri il 9 Mag 2021
you could do something like this:
isb=6;
nta_matrix = zeros(3,1); %initializing, it usually saves memory.
for i=1:3
nta_matrix(i) = (1+isb).^2/isb;
isb=nta_matrix(i);
end

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by