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.
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) ?
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…
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.
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.
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.
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
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).
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
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];
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.
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?
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.
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??
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
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) ?
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
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.
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…
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.
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.
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
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
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
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
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
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
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
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))
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
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
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
Thanks!
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??
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
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
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.
Steve,
Preallocation can be important for performance, and is often easy to do. Thanks for the reminder.
Doug