Finding the Maximum Value in a Matrix Using MATLAB While Command

  • MATLAB
  • Thread starter Dell
  • Start date
  • Tags
    Matlab
In summary, the conversation discussed the use of loops in MATLAB, specifically the while command. The participants also shared an exercise in which they had to find the highest value in a given matrix. One participant shared their code using nested while loops and an if command, while the other participant found an error in their code and shared a revised version.
  • #1
590
0
we recently learned about loops in matlab, more specifically the while command, from what i understand, MATLAB will continue with the set command as long as the condition i chose is true.

as an excercise we had to make a program that will give me the highest value in a given matrix

what i tried doing was:

a=rand(5)
n=1
max=a(n)
while a(n)<max
n=n+1;
max=a(n);
end

what i thought this would do was take a random 5x5 matrix, start from a(1,1) and find the largest value,,, not so,

how can i make such a program using "while", not "if", what i want it to do is take a loop and keep on adding 1 to the previous n, keeping the largest value as max
 
Physics news on Phys.org
  • #2
Well the first problem is that a is a 5x5 array and by using a(n) you are only accessing the nth row of the matrix rather than each individual element. To examine each element you need to use two nested while loops as well as an if command.

Something like the following should work,

Code:
max = 0; m=1; n=1;
matrix = rand(5);

while m <= size(matrix,1)
     while n <=size(matrix,2)
          if matrix(m,n) > max
               max = matrix(m,n);
          end
          n=n+1;
     end
     m=m+1;
end

max
If you don't understand any of the commands that I have used, or don't understand why I have used them please do ask.
 
Last edited:
  • #3
Hootenanny said:
Well the first problem is that a is a 5x5 array and by using a(n) you are only accessing the nth row of the matrix rather than each individual element. To examine each element you need to use two nested while loops as well as an if command.
QUOTE]

thanks, but i learned that MATLAB stores the matrix as a column, therefore if i have a 5x5 matrix "a" and want the 3rd element of the 2nd row, i can either ask for a(2,3) or a(12), check it out, it works,,
maybe when dealing with conditions this is not allowed but i don't see why not,

either way your way still didnt find the max value
 
  • #4
thanks for the help, got it

a=rand(5);
n=1;
mx=0;
b=size(a);
while n<=b(1)*b(2);
if mx<a(n);
mx=a(n);
end
n=n+1;
end

mx
 
  • #5
Dell said:
thanks, but i learned that MATLAB stores the matrix as a column, therefore if i have a 5x5 matrix "a" and want the 3rd element of the 2nd row, i can either ask for a(2,3) or a(12), check it out, it works,,
maybe when dealing with conditions this is not allowed but i don't see why not,
I didn't know that, but it's something that I'll have to remember! Thanks.
Dell said:
thanks for the help, got it

a=rand(5);
n=1;
mx=0;
b=size(a);
while n<=b(1)*b(2);
if mx<a(n);
mx=a(n);
end
n=n+1;
end

mx
Excellent - you did it even without my help :redface:

Anyway, I found the error in my code. The column counter n needs to be reset for each new row m:
Code:
max = 0; m=1; n=1;
matrix = rand(5);

while m <= size(matrix,1)
     while n <=size(matrix,2)
          if matrix(m,n) > max
               max = matrix(m,n);
          end
          n=n+1;
     end
     m=m+1;
     n=1;
end

max
 

1. How do I find the maximum value in a matrix using the while command in MATLAB?

To find the maximum value in a matrix using the while command in MATLAB, you can use the max function. This function will return the maximum value in the matrix, and you can also specify the dimension along which you want to find the maximum value.

2. Can I use a nested while loop to find the maximum value in a matrix in MATLAB?

Yes, you can use a nested while loop to find the maximum value in a matrix in MATLAB. You can use the while loop to iterate through the rows and columns of the matrix and use the max function to find the maximum value in each row or column.

3. Is the while command the most efficient way to find the maximum value in a matrix in MATLAB?

No, the while command may not be the most efficient way to find the maximum value in a matrix in MATLAB. You can also use other functions such as max, maxk, or sort to find the maximum value in a matrix. The most efficient method may depend on the size and structure of your matrix.

4. Can I find the maximum value in a matrix with a specific condition using the while command in MATLAB?

Yes, you can use the while command in MATLAB to find the maximum value in a matrix with a specific condition. You can use logical indexing to specify the condition and then use the max function to find the maximum value in the filtered matrix.

5. How can I handle missing values while finding the maximum value in a matrix using the while command in MATLAB?

To handle missing values while finding the maximum value in a matrix using the while command in MATLAB, you can use the NaN function to replace the missing values with NaN (Not a Number). Then, you can use the max function with the 'omitnan' option to ignore the NaN values and find the maximum value in the matrix.

Suggested for: Finding the Maximum Value in a Matrix Using MATLAB While Command

Replies
4
Views
455
Replies
5
Views
993
Replies
5
Views
919
Replies
8
Views
1K
Replies
4
Views
2K
Replies
1
Views
1K
Replies
32
Views
2K
Replies
2
Views
2K
Replies
2
Views
1K
Back
Top