52 views (last 30 days)
Show older comments
adam on 6 Mar 2014
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results
Commented: Dyuman Joshi on 28 Feb 2024
Accepted Answer: Thomas
Open in MATLAB Online
Hi guys, have tried searching but can't find anything to help, maybe my problem is too simple! lol Anyway, I'm running a nested FOR loop, but the array I save my results to only keeps the last "run" of results. Can someone please help me to store/concatenate the results in a single array? EG the resultant array should have 12 rows, not 4. with column one going 1,2,3,4,1,2,3,4,1,2,3,4 and column two going 10,10,10,10,20,20,20,20,30,30,30,30.
Cheers in advance! (first line currently commented out as the last 8 rows stay zero at the minute)
%tableA = zeros(12,2);
for i=1:4
for j=1:3
answerA=i*1
answerB=j*10
tableA(i,:)=[answerA answerB]
end
end
P.S. this isn't the real code I'm using, but a dumbed down version just so I can get the correct syntax to use to apply to the bigger problem! Cheers.
2 Comments Show NoneHide None
Show NoneHide None
Kyle on 15 Jul 2015
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results#comment_298347
Open in MATLAB Online
tableA = zeros(12,2); for j = 1:4
for m = 1:4
for i = 1:4
answerA = i;
answerB = 10;
tableA(i,:) = [answerA answerB];
end
answerA = m;
answerB = 20;
tableA(m+4,:) = [answerA answerB];
end
answerA = j;
answerB = 30;
tableA(j+8,:) = [answerA answerB];
end
There is probably an easier way, but this works.
Aroosh Amjad on 13 Feb 2017
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results#comment_428452
any body can give me exact code for storing values in "ForLoop".? i would be very thankful to you
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Thomas on 6 Mar 2014
⋮
Edited: MathWorks Support Team on 28 Nov 2018
Open in MATLAB Online
for i=1:4
for j=1:3
answerA(i,j)=i*1;
answerB(i,j)=j*10;
% tableA(i,:)=[answerA answerB]
end
end
table=[reshape(answerA,[],1) reshape(answerB,[],1)]
2 Comments Show NoneHide None
Show NoneHide None
adam on 6 Mar 2014
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results#comment_200271
thanks mate, works a treat!
Adithya Prabhu on 22 Dec 2017
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results#comment_518683
can you please explain how this works?
Sign in to comment.
More Answers (4)
dpb on 6 Mar 2014
Open in MATLAB Online
As this is clearly early learning, let's go with a hint-based approach rather than just throwing out an answer -- might make it more beneficial to discover the answer sorta' on your own...
%tableA = zeros(12,2);
for i=1:4
for j=1:3
answerA=i*1
answerB=j*10
tableA(i,:)=[answerA answerB]
...
1) preallocating is a_good_thing (tm) so bring that back out of retirement but -- first, for ease use a variable for the upper limit on the two loops so you can modify them and easily compute the size of the array needed--
nr=4; % rows
nc=3; % columns
table=zeros(nr,nc); % preallocate
2) NB: that answerA is invariant with j so move it outside the inner loop--no sense doing stuff over and over that is the same answer every time--
for i=1:nr
answerA=i*1; % of course the '*1' isn't needed but it's tutorial, so ok...
3) Now to the nub of your ? --
for j=1:nc
AnswerB=j*10; % NB: this doesn't give what you say you want, either...
table(?,?) = ???
Walk thru the steps and follow what i is each pass -- it should be apparent what the next row index should be; what computation would generate that?
After that, then look at how to get the indices for the two values in the proper columns -- it's a similar idea. Or, of course, instead of building a vector to store two at a time, store the individual i,j elements in their correct location when generate them is more direct.
4) Rethink the whole process and see if you cannot actually compute the whole thing in a vectorized form and eschew the loops entirely. That's "the Matlab way".
Chew on that a while and then come back... :)
2 Comments Show NoneHide None
Show NoneHide None
adam on 6 Mar 2014
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results#comment_200331
thanks for the reply, unfortunately I'm not too sure what you're on about lol. The vectorising sounds interesting though - I'll try and do some googling on that if I have time. Cheers
Dyuman Joshi on 28 Feb 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results#comment_3081466
@Manuel Campos notes on @dpb's answer - "Thorough explanation and the solution."
Sign in to comment.
PANKAJ PANDYA on 3 Oct 2016
Edited: PANKAJ PANDYA on 3 Oct 2016
Open in MATLAB Online
if true
% i think you would like it
% i liked the question. it really checked my aptitude
table=zeros(12,2);
for k=0:4:8
for i=1:4
for j=1:2
if mod(j,2)==0
table(i+k,j)=i;
else
table(i+k,j)=10*(k/4+1);
end
end
end
end
end
it works fine
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sawan Kumar Jindal on 3 Oct 2016
Edited: Sawan Kumar Jindal on 3 Oct 2016
Open in MATLAB Online
The first thing to do is assign zero value to each location of the matrix and then, use for loops to store the value. You have not initialised the memory for all the locations.
Code ex:
for i=1:4
for j=1:3
answerA(i,j)=i*1;
answerB(i,j)=j*10;
tableA(i,:)=[answerA answerB]
end
end
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
adam on 6 Mar 2014
Open in MATLAB Online
Hi guys,
the first answer worked great, however I can't use a step size less than 1 (as indices can't be less than 1 right?)
Do I need a different method in order to achieve what the below code is trying to do?
(I just get "??? Attempted to access answerA(1.5,1); index must be a positive integer or logical." error)
for i=1:0.5:4
for j=1:3
answerA(i,j)=i*1;
answerB(i,j)=j*10;
end
end
table=[reshape(answerA,[],1) reshape(answerB,[],1)]
Moving my answerA outside the j loop didn't work either, but that's a minor thing.
Cheers
2 Comments Show NoneHide None
Show NoneHide None
adam on 6 Mar 2014
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results#comment_200333
Open in MATLAB Online
sorry guys, think I've sorted it. Just added an index value as below:
index=0;
for i=1:0.5:4
for j=1:3
index=index+1;
answerA(index)=i*1;
answerB(index)=j*10;
end
end
table=[reshape(answerA,[],1) reshape(answerB,[],1)]
Any thoughts or comments are still greatly appreciated though! Cheers Adam
dpb on 6 Mar 2014
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/120444-storing-values-from-nested-for-loop-array-only-saves-last-run-of-results#comment_200352
BINGO!!! on that lesson... :)
Note however you still have answerA computed 24 times when it's only needed to be done 8 by not moving the j-invariant portion of the code outside the loop on j
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and ArraysMatrix Indexing
Find more on Matrix Indexing in Help Center and File Exchange
Tags
- nested
- for loop
- store values in array
- array
- concatenate array
Products
- MATLAB
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office