0

Hello I have 3 variables i,j and k. I want to run a loop on each of them such that for each value of i, one value of j is considered and iterated over all values of k.Then for same i, take second value of j and iterate over all values of k and so on... Then repeat this process for all the values of i and save the output. For example for i=1,j=1 iterate over all values of k.Then for i=1, j=2 iterate over all values of k so on. Then save the output m for all the values of i and j over k iterations.

for i=1:30
for j=1:5
for k=1:100
    m=i*0.5*j*sin(k);
end
end
end

May be my code is not right as well?

1
  • Many thanks for your time but that's not what I am looking for. I have edited my question in more detail. Can you please check if you have time. Many Thanks Commented Nov 12, 2015 at 15:23

3 Answers 3

3

Using ndgrid you can accomplish this without any explicit for loops:

[i,j,k] = ndgrid(1:30,1:5,1:100);
m = 0.5*i.*j.*sin(k);

Or with meshgrid if you flip the first two inputs:

[j,i,k] = meshgrid(1:5,1:30,1:100);
m = 0.5*i.*j.*sin(k);
Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks for your time but that's not what I am looking for. I have edited my question in more detail. Can you please check if you have time. Many Thanks
@bilal: This code exactly replicates the output of the code in your question (except that it stores all values for each combination of i, j, and k in a 3-D array). You need to update that too. The updated text in your question is not clear at all.
Perhaps my code is not right. I want to find m for i=1,j=1 over k iterations, then i=1,j=2 and for all values of k and so on. Does this make any sense?
[i,j,k] = ndgrid(1:30,1:5,1:100); m = 0.5*i.*j.*sin(k); then m(1,1,:) is a vector for i=1, j=1, and all values of k (1 to 100). From your description, I don't see what else you could want.
2

The easiest thing you can do is to define m as a 3D array; also, since you know in advance the final size of the array you can initialize it before the loops to avoid MatLab allocating the memory at each iteration:

% Initialize the array
m=zeros(30,5,100);
for i=1:30
   for j=1:5
      for k=1:100
         % Set the values in the array "m"
         m(i,j,k)=i*0.5*j*sin(k);
      end
  end
end

EDIT

In case you want, for each i and j the value of m be computed over k iterations, the variable m should be present also on the right of the = sign.

It should, for example, be somenthing like:

m=m+i+j+k

you can try the following modified version of the above code

% Initialize the array
m=zeros(30,5);
for i=1:30
   for j=1:5
      % Initialize the temporary varaible at each iteration
      tmp_var=0;
      for k=1:100
         % Compute the value of "tmp_var" over "k" iteration
         tmp_var=tmp_var+i*0.5*j*sin(k);
      end
      % Assign the value of "tmp_var" computed for a given (i,j) couple
      m(i,j)=tmp_var;
  end
end

In this example you need to define a temporary varaible to be used inside the k loop; at the end of the k loop the value of the temporary variable will be stored in the m matrix.

At the end of the script, you will have a matrix m of size (30 x 5).

Hope this helps.

5 Comments

Many thanks for your time but that's not what I am looking for. I have edited my question in more detail. Can you please check if you have time. Many Thanks
Unfortunately the additional information you've added are not clear: for a given value of i and j does the value of m at the k iteration somehow depend on the value of m at the (k-1) iteration?
Perhaps my code is not right. I want to find m for i=1,j=1 over k iterations, then i=1,j=2 and for all values of k and so on. Does this make any sense?
I've updated my answer, hope I've undersood you need.
Thanks for your update. But tmp-var value is added on every iteration of k ? I want to get all the values of k . Does it make any sence? Much appreciated
0

In cases where the computation in the loop is only depending on the loop index you could just write:

[j,i,k]=meshgrid(1:5,1:30,1:100);
m=.5*i.*j.*sin(k);

meshgrid will create the appropriate 3D arrays of the indices i,j,k and m will be of the same dimension when the elementwise multiplication (.* instead of *) is used.

1 Comment

Many thanks for your time but that's not what I am looking for. I have edited my question in more detail. Can you please check if you have time. Many Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.