how do i store for loop values?

조회 수: 102 (최근 30일)
row row
row row 2021년 8월 29일
댓글: row row 2021년 8월 29일
i have 3x920 double that call A
for k=1:length(A)-1 %% same as 1:919
Ax = A(2, k+1) - A(2,k)
end
Why is there only one value in the workspace?
i want something that 1x919 double
ex) Ax | 1x919

채택된 답변

Turlough Hughes
Turlough Hughes 2021년 8월 29일
편집: Turlough Hughes 2021년 8월 29일
There's only one value because your code is working as follows:
for ii = 1:3
A = ii + 1
end
A = 2
A = 3
A = 4
Matlab is not told where to store the data inside A, so A(1) just gets changed each time the loop iterates. You need to use the loop variable, ii, to indicate the index position in A where you would like to store data for the current iteration.
Let's try this again:
for ii = 1:3
A(ii) = ii + 1
end
A = 2
A = 1×2
2 3
A = 1×3
2 3 4
We can do better though; it's important to preallocate the space to your variable when it's possible to do so:
A = zeros(1,3)
A = 1×3
0 0 0
for ii = 1:3
A(ii) = ii + 1
end
A = 1×3
2 0 0
A = 1×3
2 3 0
A = 1×3
2 3 4
  댓글 수: 1
row row
row row 2021년 8월 29일
wow. thanks for u r adivce :)

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by