# MATLAB - Simple loop?

1. Nov 13, 2008

### ephedyn

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++?

2. Nov 13, 2008

### f95toli

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) )

3. Nov 15, 2008

### ephedyn

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?

4. Nov 15, 2008

### f95toli

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.

5. Nov 17, 2008

### ephedyn

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):

Thanks!

6. Nov 20, 2008

### zyh

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.

7. Dec 10, 2008

### ephedyn

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.