Doug's MATLAB Video Tutorials

August 20th, 2007

MATLAB Basics video: Storing data in a matrix from a loop

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. video.jpg 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/

39 Responses to “MATLAB Basics video: Storing data in a matrix from a loop”

  1. kay replied on :

    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) ?

  2. Doug replied on :

    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

  3. DH Cho replied on :

    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
    x=xlsread(fileName, i, range);
    x=[x xlsread(fileName, i+1, range)];
    end

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

    How should I solve this problem?

    sincerely from novice.

  4. JJ Garza replied on :

    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…

  5. Steve L replied on :

    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.

  6. Joseph Alvarado replied on :

    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);
    Please help. Thank you.

  7. Doug replied on :

    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

  8. Dimitris replied on :

    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

  9. dhull replied on :

    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

  10. Dimitris replied on :

    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

  11. dhull replied on :

    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

  12. Raghu replied on :

    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

  13. dhull replied on :

    Raghu,

    You need to store this data into a 2-d matrix, right now, you are saving it into a vector. Essentially, you will need to do something like this:

    trac(i,j) = trac_1;

    I recommend watching this video also:

    http://blogs.mathworks.com/videos/2007/08/31/matlab-basics-video-row-and-column-indexing/

    -Doug

  14. Jeanette replied on :

    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))

  15. dhull replied on :

    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

  16. Jeanette replied on :

    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

  17. dhull replied on :

    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

  18. Jeanette replied on :

    Thanks!

  19. Davide replied on :

    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??

  20. Nadine replied on :

    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

  21. dhull replied on :

    Nadine,

    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

  22. Steve L replied on :

    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.

  23. dhull replied on :

    Steve,

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

    Doug

  24. Matteo replied on :

    Another great tutorial Doug!
    And great comments.

    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

  25. dhull replied on :

    @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

  26. matteo replied on :

    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…
    :-)

  27. dhull replied on :

    @matteo

    http://blogs.mathworks.com/videos/2009/02/27/dynamic-field-name-usage/

    This gives you a similar idea for a structure.

    For a cell

    a{1} = [4 8 15]
    a{2} = [16 23 42]

    This could be done in a for loop.

    Doug

  28. vinay bansal replied on :

    dear sir/madam ,
    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;
    
  29. Doug replied on :

    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;
    
  30. vinay bansal replied on :

    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 ?

  31. dhull replied on :

    @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

  32. vinay bansal replied on :

    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 ?

  33. dhull replied on :

    @vinay,

    Please watch this:

    http://blogs.mathworks.com/videos/2007/08/31/matlab-basics-video-row-and-column-indexing/

    Doug

  34. vinay bansal replied on :

    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.

  35. dhull replied on :

    >> m = magic(3)

    
    m =
    
         8     1     6
         3     5     7
         4     9     2
    
    >> r = find((m(:,1) == 8) & (m(:,2) < 3))
    
    r =
    
         1
    
  36. Tachibana49 replied on :

    Thanks for your tutorial, Doug.
    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.

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

    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.

  39. dhull replied on :

    @Santiago

    m(loopNumber,:) = vec

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

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