Finding the Maximum Value in a Matrix Using MATLAB While Command

  • Context: MATLAB 
  • Thread starter Thread starter Dell
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around finding the maximum value in a matrix using the "while" command in MATLAB. Participants explore different approaches to iterate through a 5x5 matrix and identify the largest element, addressing issues related to indexing and loop structure.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their initial attempt to find the maximum value using a single while loop, expressing confusion about why it did not work as expected.
  • Another participant points out that accessing a(n) only retrieves the nth row of the matrix and suggests using nested while loops along with an if statement to check each element.
  • A participant challenges the previous claim by stating that MATLAB allows accessing elements in a matrix using a single index, indicating that their understanding of MATLAB's storage of matrices as columns supports their approach.
  • One participant successfully implements a solution using a single while loop and confirms that it works, while another participant acknowledges a mistake in their earlier code regarding resetting the column counter for each new row.

Areas of Agreement / Disagreement

Participants express differing views on the correct method to access matrix elements and whether a single while loop can be effectively used to find the maximum value. The discussion reflects multiple competing approaches and remains unresolved regarding the best practice.

Contextual Notes

Some participants highlight the need for nested loops to access individual elements, while others assert that a single index can suffice. There is also mention of resetting loop counters, indicating potential confusion about loop structure and indexing in MATLAB.

Dell
Messages
555
Reaction score
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 exercise 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
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:
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
 
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
 
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K