Store data into new variable in for loop
32 ビュー (過去 30 日間)
古いコメントを表示
Suppose I have a for loop function, I want to store the result into a new variable once the loop iterates. How can I implement that?
n = user input
for i = 1:n
result = 10 - i;
Datai = result;
end
The output would look like this
n=5
Data1 = 9
Data2 = 8
Data3 = 7
Data4 = 6
Data5 = 5
4 件のコメント
Stephen23
2017 年 10 月 22 日
編集済み: Stephen23
2017 年 10 月 22 日
" I want to store the result into a new variable once the loop iterates"
While it is possible to magically create variables, doing so makes code complex, slow, buggy, and hard to debug. Read this to know more:
Much better is to simply use indexing: indexing is much simpler, very fast, and very efficient.
dpb
2017 年 10 月 22 日
Good catch on the multi-variable part; I completely glossed over the output list OP posted not reading it carefully enough to note it wasn't actually the output of the code but wished-for...his would have produced 'Datai = n' as list each iteration.
Which raises question of where in the documentation should the examples be that would make this beginner faux pas less prevalent an occurrence? It clearly isn't highly-enough visible given the frequency that doesn't appear to decline much.
Was also struck by the similarity to <how-to-store-values-in-an-array-from-a-loop> of just a few days ago which is another theme; are there not sufficient for loop examples and indexing expressions?
採用された回答
KL
2017 年 10 月 22 日
編集済み: KL
2017 年 10 月 22 日
It's not a good idea to create such dynamic variables inside loop. How about this?
Data = zeros(1,n);
for k = 1:n
Data(1,k) = 10-k;
end
so when n=5
Data = [9 8 7 6 5]
3 件のコメント
KL
2017 年 10 月 22 日
I should have preallocated, yes. Thanks for pointing it out dpb. Edited my answer now.
dpb
2017 年 10 月 22 日
No problem; just the end result became a case of "Do what I say, not what I do!" caught my attention as kinda' a chuckle...
I am (and was) sure it was just oversight; hence the grins...
その他の回答 (1 件)
dpb
2017 年 10 月 22 日
編集済み: dpb
2017 年 10 月 22 日
For Matlab, you wouldn't need a loop at all...
N=5;
data = 10-[1:N];
as a constant can be added onto a vector automagically.
If there's something else going on in the loop that really must use the loop, then you want to first "preallocate" the storage for the result and then populate the array...
data=zeros(N,1); % preallocate array/vector of Nx1 elements
for i = 1:N
Data(i) = 10-i;
end
Purists will note that i is builtin function in Matlab and defined as the imaginary i of complex variables and so that it is not "best practice" to use it as an iteration variable. But, the general practice is so ubiquitous that it is still quite common; just be aware of the possible ramifications in some other contexts.
2 件のコメント
dpb
2020 年 6 月 9 日
編集済み: dpb
2020 年 6 月 9 日
Use cell arrays would be general answer.
But, an example of what you're trying to do would confirm/deny that.
BTW, better to ask new Q? than diverge too far from original subject on a previous...
But, see if an earlier Answer today will help <Answers/545081 Results-in-a-for-loop-where-the-number-of-peaks-are-not-the-same-for-every-vector>. I took liberty of modifying the title a little to address the fundamental Q? raised here irrespective of how the other poster got the result...which is appropriate here as well as we're not given that information.
参考
カテゴリ
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!