Store data into new variable in for loop

33 views (last 30 days)
Kim Lopez
Kim Lopez on 22 Oct 2017
Edited: dpb on 9 Jun 2020
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
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
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?

Sign in to comment.

Accepted Answer

KL
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
KL on 22 Oct 2017
I should have preallocated, yes. Thanks for pointing it out dpb. Edited my answer now.
dpb
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...

Sign in to comment.

More Answers (1)

dpb
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
ROCKTIM JYOTI DAS
ROCKTIM JYOTI DAS on 9 Jun 2020
But the data I want to store itself is a matrix?
dpb
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!