how do i store for loop values?
68 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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
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)
for ii = 1:3
A(ii) = ii + 1
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!