This video shows how to fix common errors in MATLAB when indexing into a vector or matrix in a for loop.
By
Doug Hull
Doug first used MATLAB in 1994, could not figure it out until he got some help in 1995. He is now dedicated to making sure that no one else wastes a year of their life not knowing MATLAB like he did.
The code you suggest works, however it is extremely slow for larger problems when compared with constructing a pre-allocated array in a FOR loop. If you want to avoid explicit pre-allocation for some reason, you can often do something like this (which I call dynamic pre-allocation):
cnt = 1801;
for ii = 10:-.005:1
d(cnt) = ii*10; % d is fixed size after first pass.
cnt = cnt – 1;
end
The output of second part of your code is not the same as the first one.
The output of the firs part is from 10 to 100 but the output of the second is from 0 to 100.
This article gives the light in which we can observe the reality. this is very nice one and gives indepth Nike SBinformation. thanks for this nice article
Leave a Reply
About
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
Is there anything wrong with using the following?
@Robbie,
The code you suggest works, however it is extremely slow for larger problems when compared with constructing a pre-allocated array in a FOR loop. If you want to avoid explicit pre-allocation for some reason, you can often do something like this (which I call dynamic pre-allocation):
cnt = 1801;
for ii = 10:-.005:1
d(cnt) = ii*10; % d is fixed size after first pass.
cnt = cnt – 1;
end
@doug:
Your third case is pretty much what I do on a daily basis. However, I have found myself more comfortable with the following syntax:
I also took the liberty of preallocating memory for the sake of it.
The output of second part of your code is not the same as the first one.
The output of the firs part is from 10 to 100 but the output of the second is from 0 to 100.
Thank you for your great videos.
@Kamran,
It is never claimed they have the same results.
Doug
This article gives the light in which we can observe the reality. this is very nice one and gives indepth Nike SBinformation. thanks for this nice article