A for loop is used to construct a simple matrix with an underlying pattern. Pre-allocation is addressed in the second half of the video.
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.
Thank you, you are right and I can see the advantage to knowing that technique.
I, however, have a hard time internalizing and visualizing that kind of construction. Mentally, it takes me a little longer to figure out what bsxfun does. For small matrices like this, I prefer ease of understanding to terseness of code.
For this case, there is no appreciable performance difference, so it is a matter of taste.
FWIW, I do not make up these cases, virtually everything on this blog comes from user questions. If someone is asking me how to make that matrix, then that tells me they are not ready for bsxfun! I only bring out the more powerful (but harder to understand) techniques for the *advanced* section of this blog.
r2=1
r1=10
a=[r2:r1];
for r2 = r2:r1
G = r2/3;
sigrad2 = G*a^2;
end
This is just a simplification of a larger code but for the crux of the matter is, the for loop calculates vectors for sigrad2, but I would like to store each of the results in a matrix – I have watched the video where you build a vector with y = i+rand inside the loop but haven’t been able to extend it to my case.
I would then like to plot each of those rows in the resultant matrix against a
Could you please help me in building that matrix? It seems simple but I can’t figure it out
I would use:
@Oleg,
Thank you, you are right and I can see the advantage to knowing that technique.
I, however, have a hard time internalizing and visualizing that kind of construction. Mentally, it takes me a little longer to figure out what bsxfun does. For small matrices like this, I prefer ease of understanding to terseness of code.
For this case, there is no appreciable performance difference, so it is a matter of taste.
FWIW, I do not make up these cases, virtually everything on this blog comes from user questions. If someone is asking me how to make that matrix, then that tells me they are not ready for bsxfun! I only bring out the more powerful (but harder to understand) techniques for the *advanced* section of this blog.
Thanks,
Doug
Hi Doug, great and helpful site here!
Here is the beginning of my code;
This is just a simplification of a larger code but for the crux of the matter is, the for loop calculates vectors for sigrad2, but I would like to store each of the results in a matrix – I have watched the video where you build a vector with y = i+rand inside the loop but haven’t been able to extend it to my case.
I would then like to plot each of those rows in the resultant matrix against a
Could you please help me in building that matrix? It seems simple but I can’t figure it out
Thanks in advance
@Ravi
http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/
@dhull
Yeah I mentioned that I saw that video although that only builds a 1×10 vector but I’m not sure how to expand that code to a mxn matrix
@Ravi,
Add a second loop outside that one that counts across the columns.
Doug