A simple for loop question

조회 수: 5 (최근 30일)
Ulrich Achas
Ulrich Achas 2021년 5월 9일
댓글: Ulrich Achas 2021년 5월 9일
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
Jonas
Jonas 2021년 5월 9일
use nta(i) in both cases
Ulrich Achas
Ulrich Achas 2021년 5월 9일
thank you.

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

채택된 답변

Mazdack Ameri
Mazdack Ameri 2021년 5월 9일
편집: Mazdack Ameri 2021년 5월 9일
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
  댓글 수: 1
Ulrich Achas
Ulrich Achas 2021년 5월 9일
it worked, thank you so much.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by