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
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
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
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
KL 2017-10-22
I should have preallocated, yes. Thanks for pointing it out dpb. Edited my answer now.
dpb
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
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 个评论
ROCKTIM JYOTI DAS
But the data I want to store itself is a matrix?
dpb
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 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