Storing Outputs from a Nested Loop

I have the following problem, wherein, i want to store outputs from the nested loops. I am just presenting a simple scenario to depict the situation:
for a=[1:1:10]
for b=[1:1:10]
c=a*b
end
end
c in this case would store the last value i.e. 100. How to develop an array which stores all the values of c from 1 to 100 as c(1,1)=1, c(1,2)=2 and so on.

 Akzeptierte Antwort

Iddo Weiner
Iddo Weiner am 31 Jan. 2017

1 Stimme

By initiating an empty array and then filling it with the looping indices:
c = nan(10,10);
for a = 1:10
for b= 1:10
c(a,b)=a*b;
end
end
% visualize c
imagesc(c)

8 Kommentare

A M
A M am 31 Jan. 2017
Thank you for this support. I am still stuck. Can you look into this code:
c=nan(5,5);
for a=[20:20:100];
for b =[5:-1:1];
c(a,b)=(a+b);
end
end
Please correct me. To me belief, this should have returned a 5x5 matrix with all values stored in it such that the result of c(20,5)=25 is appeared. Instead it shows that 'c' is a 5x100 with 0 stored in 1-19 and then values appearing at 20. How to solve this issue so i can have a clean matrix of 5x5.
Stephen23
Stephen23 am 31 Jan. 2017
Bearbeitet: Stephen23 am 31 Jan. 2017
A = 20:20:100;
B = 5:-1:1;
C = NaN(numel(A),numel(B));
for a = 1:numel(A)
for b = 1:numel(B)
C(a,b) = A(a)+ B(b);
end
end
You were using the values as indices. Instead you need to create separate indices and use those to index into the vectors and output matrix.
Jan
Jan am 31 Jan. 2017
@A M: Do you see, that the suggested codes contain less square brackets? See http://www.mathworks.com/matlabcentral/answers/35676-why-not-use-square-brackets .
A M
A M am 31 Jan. 2017
Thank you. This appears to be a promising solution. It worked out and i got a 5*5 matrix. So, if i am not mistaken, the NaN creates a n*n matrix. What does the 'Numel' exactly does? Is it not the same as NaN(5,5) creating a 5*5 matrix?
Jan
Jan am 31 Jan. 2017
@A M: See
doc numel
Niels
Niels am 31 Jan. 2017
Matlab uses (as you should also do) smart shortcuts for its functions and variables, such as numel for Number of Elements
I think you are supposed to get a 10*10 matrix in the example you posted. Yes, nan() initiates an empty array. You can also use zeros() to initiate a matrix of zeros. BTW - this code works also without the imitation line, i.e.:
for a = 1:10
for b= 1:10
c(a,b)=a*b;
end
end
BUT this is highly un-recommended, because the shape of c changes constantly - which is a very inefficient use of memory. When you pre-allocate then c's shape is constant and the whole code runs much quicker (of course in the example case the array is very small and there won't be a significant difference).
Hope this helps, good luck

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Niels
Niels am 31 Jan. 2017

1 Stimme

some changes:
aMax=10; % equal to #of rows
bMax=10; % equal to #of colums
c=zeros(aMax,bMax);
for a=1:aMax % let a run from 1 to aMax, stepwidth:=1
for b=1:bMax
c(a,b)=a*b; % hope you know that c is not running fom 1 to 100
end
end
if you want c to cointain to values 1:(aMax*bMax) (100 in current state) change
c(a,b)=(a-1)*aMax+b;

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

A M
am 30 Jan. 2017

Kommentiert:

am 1 Feb. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by