Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB - Simple loop?

  1. Nov 13, 2008 #1
    OK, I've a much more complicated code, but I've simplified the below to portend to my question more specifically.

    Basically, I want to store some arrays as variables in the following manner:
    q1 = 2
    q2 = 3
    q3 = 4
    q9 = 10
    q10 = 11

    Does anyone know what I should use instead of the parentheses "[ ]" which would have worked in C++?
    Thanks in advance!
  2. jcsd
  3. Nov 13, 2008 #2


    User Avatar
    Science Advisor
    Gold Member

    Use () instead of [].
    Also, you can't use 0 as an index in Matlab; i.e. the first element in an array is element 1 (in your case q(1) )
  4. Nov 15, 2008 #3
    Hmm, thanks for the help, it came up with a 1x10 matrix which would have been good for my stated problem, but I think what I really need are 10 1x1 matrices. I apologise that I didn't elaborate further as I'm not sure if I can clearly express the full problem, but maybe it will make more sense to you if I try to elaborate what I'm doing anyway:

    I have 2 M-files, 1 containing a PDE geometry (order5gloop), and 1 containing its boundary conditions (order5bloop).

    There's obviously something wrong with the above as I'm not sure where to place the "for" command.

    Manually, I would have filled in a real number as "r" and used the following commands to solve the PDE:

    But I would prefer if I could loop for r=1:0.01:4 (writing 400 of these is impractical), and put each set of results (this gives me 3 columns: x, y, u) as variables C1, C2, C3, C4, C5... i.e. C(r). It makes my life easier to have different matrices... I think, as I will have to calculate the standard deviation of [u_(n) - u_(n-1)]/[y_(n) - y_(n-1)] where x=2 of each C.
    Would anyone kindly know where I could start the loop command and what I should do for the C1, C2... etc. problem?

    Thanks in advance!
  5. Nov 15, 2008 #4


    User Avatar
    Science Advisor
    Gold Member

    But why would you use different matricies? Why not just a single matrix and an index (in this case a 1D matrix, i.e. an array)?
    It might be conceptually slightly more difficult to understand but it is definitly more efficient.
    Also, why can't you simply use a loop when you calculate the standard devition and store the result in a new vector?

    Moreover, note that you can use 3D matricies in Matlab meaning there is nothing stopping you from storing many 2D matrices in a single 3D matrix (with one index use as a "label").

    Generally speaking you should ALWAYS use matricies when you can in Matlab and preferably also matrix operations this are much more efficent than for-loops.
  6. Nov 17, 2008 #5
    OK, I thought it through and realized that you're right about using a single 3D matrix. Your help has been much appreciated.

    My concern now is how to implement this. You mentioned that "matrix operations are much more efficient than for-loops [for this]", could you give me a nudge in the direction to get started? My for-loop still doesn't work, and I don't mind abandoning it for whatever is more efficient. I tried changing the head (see the blue parts) and introducing the (r):

  7. Nov 20, 2008 #6


    User Avatar

    What do you want to do with matrix operation. I read you post, but I'm sorry I can't understand your question. Please give a more specific piece of code. The m file you give us is too long to understand.
  8. Dec 10, 2008 #7
    Sorry that I didn't reply for a long time (it would be rude not to considering you've offered your help and read through my post), I've been busy with my medical checkups.

    I can't explain my problem any better; I'm trying to settle it myself instead and work towards using a 3D matrix as suggested.

    Nonetheless, thanks.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook