Store data into new variable in for loop
35 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 22 Oct 2017
Edited: Stephen23
on 22 Oct 2017
" 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
on 22 Oct 2017
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?
Accepted Answer
KL
on 22 Oct 2017
Edited: KL
on 22 Oct 2017
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 Comments
KL
on 22 Oct 2017
I should have preallocated, yes. Thanks for pointing it out dpb. Edited my answer now.
dpb
on 22 Oct 2017
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...
More Answers (1)
dpb
on 22 Oct 2017
Edited: dpb
on 22 Oct 2017
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 Comments
dpb
on 9 Jun 2020
Edited: dpb
on 9 Jun 2020
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.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!