# MATLAB Basics video: Storing data in a matrix from a loop47

Posted by Doug Hull,

I was recently answering a question that came in from a MATLAB user on this blog. In the end, the question was really one where he was doing a calculation and storing the result in scalar Y. When he was done with the loop, he only had the final value of Y but not all of them that he had calculated on the way. The solution to this was to store each value of Y into a vector. This video is meant for new users to show how to do that.
If you would rather download the video, you can get it from the File Exchange. Also note, that I have been reformatting these videos and tagging them as videos. If you click on videos under categories to the right, you can see all the videos in one place. Other videos have been gathered here: http://blogs.mathworks.com/pick/category/video/ Other MATLAB Basics posts have been gathered here: http://blogs.mathworks.com/pick/category/matlab-basics/

kay replied on : 1 of 47

Hi,
At the end of the video you mentioned that you can do this with matrices by creating a 3D matrix. What would the indices look for that for say a 4×4 matrix?
Y(4,4,i) ?

Doug replied on : 2 of 47

Kay,

They would look like this:

>> a = rand(4,4,2)
a(:,:,1) =
0.1524 0.0782 0.0046 0.0844
0.8258 0.4427 0.7749 0.3998
0.5383 0.1067 0.8173 0.2599
0.9961 0.9619 0.8687 0.8001
a(:,:,2) =
0.4314 0.1455 0.5499 0.3510
0.9106 0.1361 0.1450 0.5132
0.1818 0.8693 0.8530 0.4018
0.2638 0.5797 0.6221 0.0760
>> a(1,2,2)
ans =
0.1455

Doug

DH Cho replied on : 3 of 47

First of all, I am resently enjoying learning Matlab skills from here. Thank you very much for your kind instructions.

Today, I have question.

As you can see below I am trying to gather data(all column vectors) from many Excel sheets.

fileName=’a.xls’
range=’A1:A500′
for i=1:39
end

“But problem is that the size is not same.”

How should I solve this problem?

sincerely from novice.

JJ Garza replied on : 4 of 47

Tutorial gave very helpful insight to my problem, but it seems to have generated a new problem in my case.
The values you display are for positive iterations, but say i wanted to do -10:10 and get the values stored at each iteration….
I realize this is not possible with the method used in the tutorial..since the values are stored in a vector and only the elements from +1 on are able to be accessed….
i’ve spent countless hours messing around with it and cant seem to find a solution….if u have any solution to this problem I would really appreciate the help…

Steve L replied on : 5 of 47

DH Cho,

Use a cell array instead of growing the vector/matrix inside the loop.
 fileName = 'a.xls'; range = 'A1:A500'; x = cell(1, 39); % Preallocation for k = 1:39 x{k} = xlsread(fileName, k, range); end 

The data from the kth sheet will be in x{k} (note the curly braces instead of parentheses.)

JJ Garza,

If you want to run your iterations from -10 to 10 and store all the data, I can think of two easy ways to do so. Method 1 involves transforming your FOR loop counter to use as indices:

 x = zeros(1, 21); % Preallocation for k = -10:10 % since we want to store k = -10 in element 1 of x % we need to add 11 to k to make it the correct index x(k+11) = k.^2; end 

Method 2 is to use the FOR loop index not as the actual iterates, but to index into another vector. For example:

 y = -10:10; x2 = zeros(size(y)); % Preallocation for k = 1:length(y) x2(k) = y(k).^2; end 

The latter scenario is useful when it would be difficult or impossible to find a way to map the iterations you want to do to the indices where you want to store the result. For instance:

 y = [2 3 5 7 11 13 17 19 23]; x = false(size(y)); for k = 1:length(y) x(k) = isprime(y(k)); end 

It would be hard to convert the elements of y into [1, 2, 3, ...] to use as indices.

Joseph Alvarado replied on : 6 of 47

Hi I am trying to write a for loop for a project and I am having trouble with the output.
Within my for loop I am invoking a function that I created in an mfile. The output of the function from the mfile is a 3×27 matrix. I am trying to save all the outputs from the for loop to create a bigger matrix (33×27) which is all the outputs from going through my for loop 11 times.

code:
for i=1:numj
coef(i)=jeq(i,jcoor,bconnect);
end
coef

I keep getting this message:
In an assignment A(I) = B, the number of elements in B and
I must be the same.

Error in ==> Main at 98
coef(i)=jeq(i,jcoor,bconnect);

Doug replied on : 7 of 47

Joseph,

Here is a similar problem:

———–

out = ones(5,3); %could be 3×27

big = [];
for i = 1: 3 %could be 11
big(:,end+1:end+3) = out
end

———

The key line: big(:,end+1:end+3) = out

I would pronounce this aloud as:

matrix named BIG, all rows and column numbers one bigger than the current number of columns through column numbers three bigger than the current number of columns will be set equal to the matrix named OUT.

This is specifiying a target inside of BIG that is the same size as the OUT matrix.

-Doug

Dimitris replied on : 8 of 47

Hi Doug,
I have one basic question, I want to use ‘for’ in order to create 20 matrices from the rows of a M(20×3) matrix.

for k=1:20
x[k]= M(k,:)
end

dhull replied on : 9 of 47

Dimitris,

You are going to have to be more specific on the format of those 20 matrices. What kind of naming convention do you want, will they be in a structure, all separate matrices? Why not leave them as is and index into them as needed? If you are going to just use them in a loop, do you need to store the separately?

-Doug

Dimitris replied on : 10 of 47

Doug
I am trying to create multiple images(one pixel per image) from an RGB matrix.

img1=RGB(1,:)
img2=RGB(2,:)

img20=RGB(20,:)

and I have big problems how to use the for in order to repeat the above, the only thing that I managed to do is to write multiple images but all of them including the whole matrix (1×20 pixels).

Do you have any suggestions

dhull replied on : 11 of 47

Dimitris,

Without knowing what you are doing in the larger project, I suspect that you are making more work for yourself by storing your data like this. Very often, loops and other flow control will be easier if the data is stored in the original matrix instead of breaking it up like this. It is not clear to me why this naming scheme is better than leaving the data in a single matrix.

That being said, you can use ASSIGNIN

for i = 1:20
assignin(‘base’,['img' num2str(i)], RGB(i,:));
end

-Doug

Raghu replied on : 12 of 47

I have some basic question i have provided a code of mine at the bottom and i wanted to create a 4×11 matrix of it, how can i do it. please let me know. The code is

r = 0.409/2;
%%
Gear_ratio = [32.558 19.065 11.862 8.25];

%%
speed = [1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000];
Tor = [9.75 11 11.75 12.1 12.75 13.4 12.6 12.6 12.6 11.35 8.5];

plot (speed,Tor);

%%
for i = 1:1:length(Gear_ratio)
for j = 1:1:length(Tor)
trac_1 = Tor(j)*Gear_ratio(i)*2/r
trac = [trac;trac_1]
end
end

plot (speed,trac);

Thank you
raghu

Jeanette replied on : 14 of 47

Thanks for the video! It helped me with a matlab assignment I have! Unfortunately, something is still off in my code (below). I was looking at each of my graphs and noticed that matlab had rounding non-zero values to zero making the graph wrong. Why is matlab doing this? Or am I doing something wrong? Thanks for you help!

p.s. We were not allowed to use the sync function in matlab, and I didn’t use a for loop for the values of n b/c I think that’s the way my prof likes it.

%% Sync Fence Wave

B=10

length=1;
freq=B;
samples=200;
step=length/samples;

for index=1:samples;
t=index*step;
n1=1;
x1=sin(2*pi*B*t-n1*pi);
y1=(2*pi*B*t-n1*pi);
if y1==0
z1=1;
else
for i=index
z1(i)=x1/y1
end
end

n2=3;
x2=sin(2*pi*B*t-n2*pi);
y2=(2*pi*B*t-n2*pi);
if y2==0
z2=1;
else
for i=index
z2(i)=x2/y2;
end
end

n3=5;
x3=sin(2*pi*B*t-n3*pi);
y3=(2*pi*B*t-n3*pi);
if y3==0
z3=1;
else
for i=index
z3(i)=x3/y3;
end
end

n4=5;
x4=sin(2*pi*B*t-n4*pi);
y4=(2*pi*B*t-n4*pi);
if y4==0
z4=1;
else
for i=index
z4(i)=x4/y4;
end
end

n5=7;
x5=sin(2*pi*B*t-n5*pi);
y5=(2*pi*B*t-n5*pi);
if y5==0
z5=1;
else
for i=index
z5(i)=x5/y5;
end
end

n6=9;
x6=sin(2*pi*B*t-n6*pi);
y6=(2*pi*B*t-n6*pi);
if y6==0
z6=1;
else
for i=index
z6(i)=x6/y6;
end
end

n7=11;
x7=sin(2*pi*B*t-n7*pi);
y7=(2*pi*B*t-n7*pi);
if y7==0
z7=1;
else
for i=index
z7(i)=x7/y7;
end
end
end

ts=.005:.005:1;

plot(ts,z1,ts,z2,ts,z3,ts,z4,ts,z5,ts,z6,ts,z7)

figure(2), plot(ts,(z1))

dhull replied on : 15 of 47

Jeanette,

I ran your code. It is not clear to me where you claim MATLAB is “rounding non-zero values to zero making the graph wrong”

I notice in your code that you have this:

y1=(2*pi*B*t-n1*pi);
if y1==0
z1=1;
else

Is this what is causing the “Rounding to Zero”?

-doug

Jeanette replied on : 16 of 47

Doug,

Thanks for taking the time to look! I don’t think that is the problem. I could be wrong! My understanding is that loop will take care of the error generated if you divide by zero. If you look at figure 2, the values from ~.01-.~.05 are zero and they should be non zero values. I had previously taken the time to look at x1, y1, and z1 by removing the semi colon, and x1 and y1 are non zero values at those indexes, but when x1/y1 is computed it gives a result of 0. Maybe my loop is the problem. Any suggestions?

Jeanette

dhull replied on : 17 of 47

Jeanette,

Look carefully at this line:

if y1==0
z1=1;
else
for i=index
z1(i)=x1/y1
end
end

Notice if y1 == 0
Then z1 is set to a scalar of value one. Then later when you set z1(i) = x1/y1, the rest of the values are filled in with zeros.

I think you want to set z1(i) = 1 instead.

I found this out by putting a breakpoint in the code and watching as each value was calculated in the loop. I then noticed the switch to a scalar, and the immediate switch back to a vector with zeros padded in there.

Doug

Jeanette replied on : 18 of 47

Thanks!

Davide replied on : 19 of 47

Hi
I’m trying to fit a matrix (55×39) using a loop and polyfit.
My problem is that and the end of the loop i just can see the last coefficients, but I would like to have all of them stored somewhere
I have tryed to use some of the code written here but it seems that polyfit doesn’t accept anyone of them.

for i=1:39;
Y = [ ];
X = [ ];
Y(:,i)=Vel(:,i);
X(:,i)=B(:,i);
P =polyfit(X,Y,8);
F=polyval(P,X);
plot(X,F,’-');
hold on;
end
Any suggestions??

Nadine replied on : 20 of 47

Hi,
I am a little stuck on this problem. I have the following:

for i = -c:c/10:c,
T = equation(i);
hold on
plot(i/c,T,)
hold off
end

I need to construct a matrix that saves each ‘T’ value per ‘i’, but I keep on getting “Attempted to access T(0); index must be a positive integer or logical.”
Any help would be appreciated

dhull replied on : 21 of 47

I do not know what the values of c are, but they are likely to make i be something other than integers. So, if you were doing something like

T(i)

it is really saying

T(0.456)

which is like saying

Give me the 0.456th value in the vector T.

I suggest you put in an indexing value something like this

ind = 0

for i = -c:c/10:c
ind = ind + 1

T(ind) = equation(i)

end

That will ensure you have integer indexes into your matrix.

Doug

Steve L replied on : 22 of 47

Another alternative to Doug’s suggestion is:

 v = -c:c/10:c; T = zeros(size(v)); % Preallocation for k = 1:numel(v) T(k) = equation(v(k)); end 

The preallocation step is very important for performance, so that T doesn’t grow (and need to be reallocated) each time through the loop.

dhull replied on : 23 of 47

Steve,

Preallocation can be important for performance, and is often easy to do. Thanks for the reminder.

Doug

Matteo replied on : 24 of 47

Another great tutorial Doug!

I have a problem and I have an inkling that ASSIGNIN may be helpful but if you could confirm and maybe give me a hint in the right direction about the syntax.

I have some geophysical data and I want to apply gaussian low pass with increasing sigma and kernel size. I’ve tried for loops but I get stuck, so I end up doing it by hand. I have to do this quite often.

To give you an idea, what I do is:

H20 = fspecial('gaussian',[100 100],20);
lowp20 = imfilter(data,H20,'replicate');
H21 = fspecial('gaussian',[105 105],21);
lowp21 = imfilter(data,H21,'replicate');
...
...
H60 = fspecial('gaussian',[300 300],60);
lowp60 = imfilter(data,H60,'replicate');


and then I want to compare each result to the original, say

mse20=mean((lowp20(:)-data(:)).^2);
mse21=mean((lowp21(:)-data(:)).^2);
...
...
mse60=mean((lowp60(:)-data(:)).^2);


You can see where this is going, tons of typing.

What I would like to be able to do is something like

for i=20:60
H(i) = fspecial('gaussian',[5i 5i],i);
lowp(i) = imfilter(data,H(i),'replicate');
end


But I am stuck with the wrong syntax and I also suspect I should create a cell array to store the resulting 41 matrices of filtered data? Any help would be great. Thank you

dhull replied on : 25 of 47

@Matteo,

I think 5i should be 5*i.

The cell array would be good if the size of the results are not constant.

referencing H20, H21 and such is more difficult. Can you stuff the H-series into a cell array also? MATLAB is much happier dealing with cell array than dynamically making up names like this.

Doug

matteo replied on : 26 of 47

Thanks Doug

I was thinking about cell array for the H series given the variable size. But is there a way to stuff them in using a for loop? Otherwhise I’m only playing the three-shell-game with my typing…
:-)

vinay bansal replied on : 28 of 47

i have some problem regarding this. i have programm as follow. from inner for loop a matrix with unknown rows and 2 column size is coming. but outer loop which is creating m number of matrix is not coming. i want to store every matrix with a subscript so that i can call them later with some name as.. x(i)….please tell me what i have to do?


m = 5;
for (i = 1 :1:m)
p  = input( 'enter the species no:' ) ;

n=10;
x = [];

for k=1:1:n
a = input('enter the subgroup no');
b = input('enter the quantity of that subgroup present ');
if ( a==0) % Type subgroup no (a) = 0 to stop entry for this compound %
break;
end;
y = [a b];
x = [x ; y];
end;
x

end;

Doug replied on : 29 of 47

Here is a modified version of this that works.

m = 5;
for (i = 1 :1:m)
p  = 1

n=10;
x = [];

for k=1:1:n
a = 1
b = 1
if ( a==0) % Type subgroup no (a) = 0 to stop entry for this compound %
break;
end;
y = [a b];
x(:,:,k) = y;
end;
x

end;

vinay bansal replied on : 30 of 47

dear sir,
Thanks for your reply . regarding above problem, we have different values of ‘a’ and ‘b’ and ‘n’ is also different for every species(p). means a matrix of n rows and 2 column will come every time from complete running of inner loop. so total 5 matrix will come. exp : [1 3; 5 6]
[6 4 ]
[ 8 9 ; 4 5]
[9 6 ;8 7 ; 5 4]
[8 5; 6 2].
i want to store these matrics…. but when i run this , it show only last matrics… is there any solution to store them like X1,X2,X3,X4,X5 ?

dhull replied on : 31 of 47

@vinay,

It is better to make a cell array than to automatically generate variable names in a series like that.

a{1} = [6 4]
a{2} = [8 9; 4 5]

Notice the curly braces.

Doug

vinay bansal replied on : 32 of 47

thanks for ur kind help sir….. it was really very helpful . i have one more problem sir. thats is as below.

i want to collect data from a matrics . exp:
%a = [ m n p ]%
a = [1 4 61;
1 5 76;
1 6 56
4 1 803;
4 5 500 ;
4 6 865;
5 1 524;
5 4 125;
5 6 265 ;
6 1 57;
6 4 341;
6 5 96];

i want to collect data for m = [1 5 6] n = [1 5 6 ]
means m=1 , n= [ 5 6]
m = 5 , n= [1 6]
m = 6, n= [1 5]
means third column of matrics [a]
will u plz like to tell me how can get it automatically by applying a loop ?

vinay bansal replied on : 34 of 47

sir, but my problem is related to “find” …i want to find some value in first column and also some other value in second column…..n then i want that row where the both value exist.

dhull replied on : 35 of 47

>> m = magic(3)


m =

8     1     6
3     5     7
4     9     2

>> r = find((m(:,1) == 8) & (m(:,2) < 3))

r =

1

Tachibana49 replied on : 36 of 47

But my problem is how to store polynomial roots into a matrix.
I got an open channel flow problem and came out with a cubic function with different coefficients at various conditions.
I tried to use loop to get the roots for all conditions and wanted to extract all roots. I used index, but MATLAB requires “In an assignment A(I) = B, the number of elements in B and I must be the same.”

So how can I store all 3 roots of each polynomial function into a matrix?
I will really appreciate your help.

dhull replied on : 37 of 47
a = [1 2 3]
b = [4 5 6]
c = [7 8 9]
M(1,:) = a
M(2,:) = b
M(3,:) = c

santiago vacca replied on : 38 of 47

Dear Doug,

I have a loop and i create a vector form each run, i want to store those vectors (in a matrix wold be fine). What´s the easy way?

Thanks very much,
Santiago.

dhull replied on : 39 of 47

@Santiago

m(loopNumber,:) = vec

rashmi replied on : 40 of 47

Dear Sir,

I have a program with nested for loops and at the end of each loops i get a value. this loop runs for around 700times. i.e. 700 bvalues and 700 time values.
now i want all these 700 generated values in a one single array/matrix. how do i do it?
%Sensor1 Cycle1
clear all
close all
clc

[M,N]=size(b2);
amp=[1.75,2,2.25,2.5,2.75,3,3.25,3.5,3.75,4];

%defining threshold
m=10501;
for i=10575:75:21000

a=b2(m:i,:);
time=b2(i,2)
t=40;
sum1=0;
sum2=0;
sum3=0;
sum4=0;
sum5=0;
sum6=0;
sum7=0;
sum8=0;
sum9=0;
sum10=0;

for R=1:75

if (a(R,3)>=t) && (a(R,3)=t+5) && (a(R,3)=t+10) && (a(R,3)=t+15) && (a(R,3)=t+20) && (a(R,3)=t+25) && (a(R,3)=t+30) && (a(R,3)=t+35) && (a(R,3)=t+40) && (a(R,3)=t+45) && (a(R,3)<t+50)
sum10=sum10+a(R,1);

end

cumulative_hits=[sum1,sum2,sum3,sum4,sum5,sum6,sum7,sum8,sum9,sum10];

for count=1:10

if cumulative_hits(1,count)<= 0
amplitude(1,count)= 0;
else
amplitude(1,count)=amp(1,count);
end

end

cumulative_hits(cumulative_hits==0)=[];
amplitude(amplitude==0)=[];

y=log10(cumulative_hits);
p=polyfit(amplitude,y,1);
f=polyval(p,amplitude);
end

%disp(p(1,1))
abs(p(1))
m=m+75;

end

doug replied on : 41 of 47

Have a doubly nested for loop.

for i = 1 : 10;
for j = 1 : 10;
m(i,j) = i + j;
end
end

Theodora Leonidou replied on : 42 of 47

Hi Doug,

I ´ve found your video very helpful. However, I would like to do something slightly more complicated and I would really appreciate if you could help me.

I am using the following code that generates a random symmetric correlation matrix C:

n=3; %Dimension of the correlation matrix
%Generating random correlation matrix
k=(rand(n)*2-1); k=(k+k’)/2; %Random symmetric matrix
C=k.*not(eye(n))+eye(n) %Principal diagonal elements set to unity

The problem is that I want generate 1000 correlation matrices with this code and that is why I need a loop. I tried to use this loop:
for i=1:1000
k(i)=(rand(n)*2-1); k=(k+k’)/2;
C=k.*not(eye(n))+eye(n)
end

But I get the following error:
In an assignment A(I) = B, the number of elements in B and
I must be the same.

Also, how is it possible to store the different C matrices in a 3-dimensional matrix?

Thank you very much!

doug replied on : 43 of 47

Just like all rows of a 2-d matrix need to be the same width, all layers of a 3-d matrix need to be the same size. Use a cell array.

Roni replied on : 44 of 47

THANK YOU DOUG! YOU ROCK!

Dee replied on : 45 of 47

Hello Doug,

I am writing a code to that requires a nested loop containing 2 for loops. The inner loop is supposed to return a 20,000 by 2 matrix that I then have to store into a 1 by 3 cell array.But at the end of the outer loop, the cell array only has data in the 3rd cell {1,3} and all other cells are empty. Thanks for your help. Below is my code:

clear all; close all; clc;
a=[0 4 0];
b=[1 7 7];
theta0=[2 1];
scale=(2.4/sqrt(2))^2;

for k=1:3
Est=cell(1,3);
x=linspace(a(k), b(k), 10)’;
n=length(x);
y=theta0(1)*(1-exp(-theta0(2)*x))+0.1*randn(10,1);
data=[x,y];
[bmin,ssmin]=fminsearch(@bod_ss,theta0,[],data);
J=[1-exp(-bmin(2).*x),x.*bmin(1).*exp(-x.*bmin(2))];
sigma2 = ssmin/(n-2); % std of measurument noise estimated by the residuals

%%%% Generate the MCMC chain
nsimu = 1000;
npar = 2;
chain = zeros(nsimu,npar);
% spherical proposal covariance, try different scales!
qcov =scale*sigma2*inv(J’*J);
R = chol(qcov); % the Cholesky ‘square root’ of qcov, for sampling

%%%% initializations
oldpar = bmin(:)’; % start the MCMC chain from the fitted LSQ/max likelihood point
chain(1,:) = oldpar;
rej = 0; % n of rejected so far …

%%%% Evaluate the SS at the starting point, and start the chain:
ss = bod_ss(oldpar,data);
SS=zeros(1,nsimu); SS(1) = ss;
%%%% start the simulation loop
for i=2:nsimu
newpar = oldpar+(R’*randn(npar,1))’; % proposed new point
ss2 = ss; % old SS
ss1 = bod_ss(newpar,data); % new SS
ratio = min(1,exp(-0.5*(ss1-ss2)/sigma2));
if rand(1,1) < ratio
chain(i,:) = newpar; % accept
oldpar = newpar;
ss = ss1;
else
chain(i,:) = oldpar; % reject
rej = rej+1;
ss = ss2;
end
SS(i) = ss; %collect SS values
end
Est(1,k)={chain};
% display the acceptance rate
disp('Acceptance rate');
accept = 1 – rej./nsimu

end
Est;

Doug replied on : 46 of 47

File Bod_ss is not included. Can you simplify code to its essence? There is a lot here that is not really part of what you are doing WRT this question.

Doug

Dee replied on : 47 of 47

Hello< sorry for my late reply. Here is bod_ss.

function ss=bod_ss(theta,data)

x = data(:,1);
y = data(:,2);

ss = sum((y – theta(1)*(1-exp(-theta(2)*x))).^2);

Thanks

What is 3 + 4?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.