Building a matrix using while loops? (MATLAB)

In summary, the conversation discusses building a matrix using "while" loops in MATLAB. The provided code uses nested while loops to iterate through the elements of three 11x11 matrices and apply a function to them. However, the code only calculates the first row of the resulting matrix and seems to end without continuing the loop. It is suggested to use for loops instead, as while loops may cause issues with keeping track of the loop variables. A possible fix for the issue is also provided.
  • #1
mikeph
1,235
18
Building a matrix using "while" loops? (MATLAB)

Hello

Here is my code:

Code:
i = 1;
j = 1; 
S = zeros(11,11);

while i < 12
    while j < 12
        S(i,j) = Test1(a(i,j),b(i,j),c(i,j));
        j = j + 1;
    end
    i = i + 1;
end

a,b,c are all 11x11 matrices and Test1 is a function m-file which outputs a number. (I have tested it for all the values that a(i,j), b(i,j) and c(i,j) take).

My code is only calculating the first row of S, ie. S(2,1) = 0 still. I don't know why the program is ending without adding 1 onto i and repeating the loop, I can only imagine the "while" loop is the wrong way of going about this?

Thanks for any help,

Mike
 
Physics news on Phys.org
  • #2


A better choice is a for loop for each of your loops.
Code:
for i=1:11
  for j = 1:11
     %% loop body
  end
end

You can do what you're doing with while loops, but they are in a sense more primitive control structures, so you have to do more of the work in your code. I think what might be happening is that after the inner loop has gone through a complete set of iterations, and i is now 2, you still have the last value of j (=12) in your inner loop. A fix for this problem might be like so:
Code:
while i < 12
    j = 1   %% added this line
    while j < 12
        S(i,j) = Test1(a(i,j),b(i,j),c(i,j));
        j = j + 1;
    end
    i = i + 1;
end
 

1. How do I create a matrix using while loops in MATLAB?

To create a matrix using while loops in MATLAB, you can use the "while" statement followed by the condition for the loop. Within the loop, you can use the "end" statement to mark the end of the loop and the "matrix" function to create the matrix. Make sure to initialize the variables and increment them within the loop to avoid an infinite loop.

2. Can I use multiple while loops to create a matrix in MATLAB?

Yes, you can use multiple while loops to create a matrix in MATLAB. You can nest the while loops within each other or use them separately, depending on your desired outcome. Just make sure to properly initialize and increment the variables for each loop to avoid any errors or infinite loops.

3. How can I add elements to a matrix using while loops in MATLAB?

To add elements to a matrix using while loops in MATLAB, you can use the "end" statement within the loop and the "matrix" function to add the elements. Make sure to properly initialize and increment the variables to ensure that the elements are added in the correct locations.

4. Is it possible to create a specific pattern in a matrix using while loops in MATLAB?

Yes, it is possible to create a specific pattern in a matrix using while loops in MATLAB. You can use conditional statements within the loop to control the values and locations of the elements being added to the matrix. Additionally, you can use the "mod" function to create repeating patterns.

5. Are there any alternatives to using while loops for creating a matrix in MATLAB?

Yes, there are alternative methods for creating a matrix in MATLAB, such as using the "for" loop or the "reshape" function. The best method to use may depend on the specific pattern or structure of the matrix you are trying to create. It is always recommended to try different methods and determine which one is the most efficient and suitable for your needs.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top