RESHAPE is a very useful function, but it is something that a lot of MATLAB users do not discover until someone is looking at their code and says “Why are you using a for loop for that?”
If you know how to use RESHAPE, there is not much to be learned from this video. If you do not know about RESHAPE, well consider me that person looking over your shoulder saying “Why are you…”
I have a quick question which is very much pertinent to your phrase “why are you using a for loop for that?” - I may be about to be on the receiving end of that statement!
I have a cell of 10000 strings of which i am identifying relevant strings using strmatch. This returns me the row numers of each match. However i require a logic return of equal dimension (1 = match). The only way i can acheieve this is using a for loop that;
a = strmatch(’abc’, data, ‘exact’)
for loop = 1:10
b(a(loop)) = 1
end
Problem is, with a huge data set this can take a long time!! Can i speed this process up??
That’s such a simple way of speeding up the process, thanks!! I have a similar issue involving the HUGE delays of ‘For Loops’. I have a date,hr,min,sec column of data however as it is in the format eg ‘04 134911′ xlsread imports as a string. I therefore have to str2num, which give a double eg [04,134911]. I only require the hr,min,sec so i then assign the (1,2) double to my data set. This works fine however is very slow with a 1:3000 ‘for loop’!! Is there a similar method that would improve the efficiency of my code??
I have a similar problem as mentioned above: my for loops appear to be really slow. I’ve read online that for loops in matlab are “slow” and “not elegant”. I’ll paste some simplified text below, but essentially I have a video (~20-30 images long) of 512×512 pixel images and want to test each pixel for certain constraints and then save data on the pixels I like. As it is it takes around 30 minutes to go through a 30-frame movie. I have 2 nested for loops doing this:
function d= get_frames(im, startframe)
for frame=framefirst:framelast;
data = get_frames(handles.im, frames);
addframe=0;
for ycount=yfirst:ylast;
addframe=0;
for xcount=xfirst:xlast;
while dwellcheck==1;
%calculate ~10 parameters (means and stds and such)
if spot>=parameter1 & spot>=parameter2 & spot>constant
after this i check to see if the same spot (x,y) in the next frame (data = get_frames(handles.im, frames+addframe);) also meets these parameters.
when checked all frames, then save spot and go back to searching for next x,y spot.
then go to next startframe.
Any advice? Or am I stuck with the speed? Thanks a lot!
Ok sweet, I played with the profiler for a while and was able to cut the program time to about half what it was (now it takes about 75 seconds to run through each image of a movie). It turns out having to do the following calculations at each point in a 512×512 pixel image take up all the time (duh):
accstdev = std([datamatrixacc(1,1:5)...
datamatrixacc(2,1) datamatrixacc(2,5)...
datamatrixacc(3,1) datamatrixacc(3,5)...
datamatrixacc(4,1) datamatrixacc(4,5)...
datamatrixacc(5,1:5)]); %takes up 32% of the time
donstdev = std([datamatrixdonor(2, 2:4) datamatrixdonor(3,2:4) datamatrixdonor(4,2:4)]); %takes up 30% of the time
spotmean3 = mean2(datamatrixacc(2:4, 2:4)); %each of these take up about 5% of the time
spotmean4 = mean2(datamatrixacc(1:5, 1:5));
spotmean1 = mean2(datamatrixdonor(2:4, 2:4));
spotmean2 = mean2(datamatrixdonor(1:5, 1:5));
I saved a lot of time by creating a new 5×5 matrix around each spot and accessing that instead of the entire 512×512 matrix. Any other recommendations? Thanks again.
I am glad the profiler is working as you need. Assuming that datamatrixacc is changing each time through the loop (no sense doing those calculations over and over on static data…) I think that things look good here.
In the profiler, it should say how many times each line is being executed. I think that each of these calculations is very quick, but you are doing them an extraordinarily large number of times.
Yeah the “datamatrixacc” is a 5×5 matrix around each individual pixel taken from the entire 512×512 matrix of the image while I run through multiple for loops (”for x=1:512″ and “for y=1:512″).
When I googled “how to make matlab faster” one thing they recommended was using vectorization instead of for loops (I guess instead of “for x=1:512″ do something like “x=1:512″ and call it each time as x(1), x(2), etc.). Will that really make anything faster, or is it going to take a while when I’m going through ~250000 pixels/frame no matter what?
It is going to take a while. The idea that vectorization is faster than for loops was once true, but with the JIT accelerator, it is mostly stylistic choice at this point.
I do not see a more clever way of doing this kind of operation. Some things are just computationally expensive.
Hi,
i have a concantenated matrix of 118×9. each row represent a particular time/frame.i want to construct a rotation matrix by extracting every row,reshaping the row into a 3×3 matrix for each row.anybody with an idea.
cheers
hello,
is there anyway i can change the dimension of my 3×3x118 rotation array so that i have a 3×354 array but with the elements in the same order ie;for every 3×3x1 element followed by 3×3x2….till 3×3x118? squeeze or reshape??help.
thanks
Doug,
thanks a lot for your replies,they are very helpful. i want to calculate the norm for mxn matrix? i have a concantenated matrix which i have to get the norm of each row. any help?
Hello,
I’m a bit of a matlab beginner and I’m hoping someone can help me. I am trying to find a way to change the ordering of where reshape puts the elements when I am going from a single row vector of many elements into an array. This may be one of those cases where I have to use a forloop instead of reshape…
Say I have vector A which is [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
If I do reshape(A,4,4,1) I get:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
But I want to reshape my vector into a matrix in a zig-zag (or snaking) pattern, instead of always starting at the top and moving down. For example, I want to “reshape” (or use some other code) to yield:
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
I need code that will perform this operation on vectors that have many, many elements. Thanks!
i am a beginner in matlab, i have some problem. i have matrix with 500×500 dimension, i want to partition in each have 20×20 dimension so there is have 25 groups matrix. what the code looping in matlab. thx
I have a question. I have a 4-D matrix like 1000×2x2×10. I reshape it to a 2-D 1000×40 matrix using:
newmatrix = reshape(oldmatrix,1000,2*2*10)
I wanted to know how is the reshape working? Basically I wanted to know what are the 40 columns in the new matrix corresponding to the original dimensions (2×2x10) of my old matrix. Does it go like:
newmatrix(:,1) = oldmatrix(:,1,1,1)
newmatrix(:,2) = oldmatrix(:,1,1,2)
.
.
newmatrix(:,11) = oldmatrix(:,1,2,1)
.
.
newmatrix(:,21) = oldmatrix(:,2,1,1)
.
.
newmatrix(:,31) = oldmatrix(:,2,2,1)
.
.
newmatrix(:,40) = oldmatrix(:,2,2,10)
Doug,
I have a quick question which is very much pertinent to your phrase “why are you using a for loop for that?” - I may be about to be on the receiving end of that statement!
I have a cell of 10000 strings of which i am identifying relevant strings using strmatch. This returns me the row numers of each match. However i require a logic return of equal dimension (1 = match). The only way i can acheieve this is using a for loop that;
a = strmatch(’abc’, data, ‘exact’)
for loop = 1:10
b(a(loop)) = 1
end
Problem is, with a huge data set this can take a long time!! Can i speed this process up??
Many thanks,
Waggy
Waggy,
How about this example?
list = {’a',’b',’c',’a',’b'}
a = strmatch(’a',list,’exact’)
b = zeros(size(list))
b(a) = 1
-Doug
Doug,
That’s such a simple way of speeding up the process, thanks!! I have a similar issue involving the HUGE delays of ‘For Loops’. I have a date,hr,min,sec column of data however as it is in the format eg ‘04 134911′ xlsread imports as a string. I therefore have to str2num, which give a double eg [04,134911]. I only require the hr,min,sec so i then assign the (1,2) double to my data set. This works fine however is very slow with a 1:3000 ‘for loop’!! Is there a similar method that would improve the efficiency of my code??
Many thanks,
Waggy
@Waggy,
Look at the profiler for your code. You may find it is not the FOR loop that is slowing things, but specific functions in the FOR loop.
Please show a simple version of your code, I bet we can figure it out if you do.
Doug
I have a similar problem as mentioned above: my for loops appear to be really slow. I’ve read online that for loops in matlab are “slow” and “not elegant”. I’ll paste some simplified text below, but essentially I have a video (~20-30 images long) of 512×512 pixel images and want to test each pixel for certain constraints and then save data on the pixels I like. As it is it takes around 30 minutes to go through a 30-frame movie. I have 2 nested for loops doing this:
function d= get_frames(im, startframe) for frame=framefirst:framelast; data = get_frames(handles.im, frames); addframe=0; for ycount=yfirst:ylast; addframe=0; for xcount=xfirst:xlast; while dwellcheck==1; %calculate ~10 parameters (means and stds and such) if spot>=parameter1 & spot>=parameter2 & spot>constantafter this i check to see if the same spot (x,y) in the next frame (data = get_frames(handles.im, frames+addframe);) also meets these parameters.
when checked all frames, then save spot and go back to searching for next x,y spot.
then go to next startframe.
Any advice? Or am I stuck with the speed? Thanks a lot!
John,
Please check this video:
http://blogs.mathworks.com/videos/2006/10/19/profiler-to-find-code-bottlenecks/
It is important to find out where your bottleneck really is. then it can be diagnosed at the line level.
Doug
Ok sweet, I played with the profiler for a while and was able to cut the program time to about half what it was (now it takes about 75 seconds to run through each image of a movie). It turns out having to do the following calculations at each point in a 512×512 pixel image take up all the time (duh):
accstdev = std([datamatrixacc(1,1:5)... datamatrixacc(2,1) datamatrixacc(2,5)... datamatrixacc(3,1) datamatrixacc(3,5)... datamatrixacc(4,1) datamatrixacc(4,5)... datamatrixacc(5,1:5)]); %takes up 32% of the time donstdev = std([datamatrixdonor(2, 2:4) datamatrixdonor(3,2:4) datamatrixdonor(4,2:4)]); %takes up 30% of the time spotmean3 = mean2(datamatrixacc(2:4, 2:4)); %each of these take up about 5% of the time spotmean4 = mean2(datamatrixacc(1:5, 1:5)); spotmean1 = mean2(datamatrixdonor(2:4, 2:4)); spotmean2 = mean2(datamatrixdonor(1:5, 1:5));I saved a lot of time by creating a new 5×5 matrix around each spot and accessing that instead of the entire 512×512 matrix. Any other recommendations? Thanks again.
John,
I am glad the profiler is working as you need. Assuming that datamatrixacc is changing each time through the loop (no sense doing those calculations over and over on static data…) I think that things look good here.
In the profiler, it should say how many times each line is being executed. I think that each of these calculations is very quick, but you are doing them an extraordinarily large number of times.
-Doug
Yeah the “datamatrixacc” is a 5×5 matrix around each individual pixel taken from the entire 512×512 matrix of the image while I run through multiple for loops (”for x=1:512″ and “for y=1:512″).
When I googled “how to make matlab faster” one thing they recommended was using vectorization instead of for loops (I guess instead of “for x=1:512″ do something like “x=1:512″ and call it each time as x(1), x(2), etc.). Will that really make anything faster, or is it going to take a while when I’m going through ~250000 pixels/frame no matter what?
Thanks for the advice.
John
John,
It is going to take a while. The idea that vectorization is faster than for loops was once true, but with the JIT accelerator, it is mostly stylistic choice at this point.
I do not see a more clever way of doing this kind of operation. Some things are just computationally expensive.
-Doug
Hi,
i have a concantenated matrix of 118×9. each row represent a particular time/frame.i want to construct a rotation matrix by extracting every row,reshaping the row into a 3×3 matrix for each row.anybody with an idea.
cheers
@dross,
Loop through each row, use reshape.
Doug
i try doing that and it says to me and it gives me this error:
>> for t=1:118;
x(t)=[i_twalk(t,:) j_twalk(t,:) k_twalk(t,:)];
A=reshape (x(t),3,3);
end
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
i do not know what is wrong.help
Dross,
Take a look at this similar problem,
m = rand(3,9)
for i = 1:3
a(:,:,i) = reshape(m(i,:),3,3);
end
a
Notice how a is becoming a 3 dimensional matrix, storing each in its own layer?
Doug
hello,
is there anyway i can change the dimension of my 3×3x118 rotation array so that i have a 3×354 array but with the elements in the same order ie;for every 3×3x1 element followed by 3×3x2….till 3×3x118? squeeze or reshape??help.
thanks
@dross,
This works,
a = rand(3,3,118);
b = reshape(a,[3 354])
You will need to be more clear with the ordering of the elements to explain more. A for loop will certainly work if reshape can not be convinced to.
Doug
Doug,
thanks a lot for your replies,they are very helpful. i want to calculate the norm for mxn matrix? i have a concantenated matrix which i have to get the norm of each row. any help?
dross,
If you have a matrix:
A = [1 2 3; 1 2 3; 1 2 3];
You can get the sub matrices
JustOnes = A(:,1)
Then you can do what ever operations you want with that.
Doug
Hello,
I’m a bit of a matlab beginner and I’m hoping someone can help me. I am trying to find a way to change the ordering of where reshape puts the elements when I am going from a single row vector of many elements into an array. This may be one of those cases where I have to use a forloop instead of reshape…
Say I have vector A which is [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
If I do reshape(A,4,4,1) I get:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
But I want to reshape my vector into a matrix in a zig-zag (or snaking) pattern, instead of always starting at the top and moving down. For example, I want to “reshape” (or use some other code) to yield:
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
I need code that will perform this operation on vectors that have many, many elements. Thanks!
We were just solving this problem internally for a contest!
Here is the solution:
n = 4
a = reshape(1:n^2,n,n)’
a(2:2:end,:) = fliplr(a(2:2:end,:));
a=a’
http://en.wikipedia.org/wiki/Boustrophedon
Enjoy,
Doug
Doug,
I just can’t seem to find any help on ‘for’ loops that actually help, would please take a look at the following and see what help you can offer?
I have 5 [1X1270] matrices that I need to perform the following equation with at each timestep:
x=((a*b)-(c*d))/e
I know it takes a for loop but I can’t get one to work. Matlab gives me an error everytime.
Charles
hello all,
i have a multidimensional array,1×1x116 and i want to re-arrange it into a 1×116 matrix.any ideas?
@dross,
Use the function SQUEEZE to do this.
Doug
@ Charles,
Since MATLAB is matrix based, you do not need a for loop to do this. You need array multiplication and array division:
x = ((a.*b)-(c.*d))./e
Notice the “.*” and “./” instead of “*” and “/”.
See this video for more info:
http://blogs.mathworks.com/videos/2009/08/27/basics-implementing-a-formula-in-matlab/
I just put it up last week. What timing!
Doug
Doug,
Appreciate your help and I’ll have a looksy at the video.
Thanks again
dear all
i am a beginner in matlab, i have some problem. i have matrix with 500×500 dimension, i want to partition in each have 20×20 dimension so there is have 25 groups matrix. what the code looping in matlab. thx
@Faisal,
What is a “groups matrix”?
Doug
i am the matlab beginer.i have to convert any n*n matrix into a single row vector.plz help
@asad,
M = magic(4)
M(:)’
Doug
Hi Doug,
I have a question. I have a 4-D matrix like 1000×2x2×10. I reshape it to a 2-D 1000×40 matrix using:
newmatrix = reshape(oldmatrix,1000,2*2*10)
I wanted to know how is the reshape working? Basically I wanted to know what are the 40 columns in the new matrix corresponding to the original dimensions (2×2x10) of my old matrix. Does it go like:
newmatrix(:,1) = oldmatrix(:,1,1,1)
newmatrix(:,2) = oldmatrix(:,1,1,2)
.
.
newmatrix(:,11) = oldmatrix(:,1,2,1)
.
.
newmatrix(:,21) = oldmatrix(:,2,1,1)
.
.
newmatrix(:,31) = oldmatrix(:,2,2,1)
.
.
newmatrix(:,40) = oldmatrix(:,2,2,10)
Appreciate your help. Thanks!
@”Crazee”
When I am faced with a problem like this, I make a simple test case and see what happens. Look at this:
a = [1 2; 3 4] A(:,:,1) = a A(:,:,2) = a+4 A(:,:,:,2) = A + 10 reshape(A,[2,8]) a = 1 2 3 4 A = 1 2 3 4 A(:,:,1) = 1 2 3 4 A(:,:,2) = 5 6 7 8 A(:,:,1,1) = 1 2 3 4 A(:,:,2,1) = 5 6 7 8 A(:,:,1,2) = 11 12 13 14 A(:,:,2,2) = 15 16 17 18 ans = 1 2 5 6 11 12 15 16 3 4 7 8 13 14 17 18