MATLAB Command "While": Find highest Value in Matrix

  • Thread starter Thread starter Dell
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around using the "while" command in MATLAB to create a program that finds the highest value in a given matrix. Participants explore the implementation of loops, indexing, and comparisons within the context of MATLAB programming.

Discussion Character

  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to use a "while" loop to find the maximum value in a random 5x5 matrix but encounters issues with their implementation.
  • Another participant questions the use of the variable "i" in the initial post, indicating a potential misunderstanding in variable referencing.
  • A clarification is made that the variable should be "n" instead of "i".
  • Further input suggests that the original approach does not allow the loop to run because the initial condition sets "max" equal to "a(n)", leading to no iterations.
  • Participants propose using the "numel()" function to determine the number of elements in the matrix and suggest a revised structure for the "while" loop to iterate through all matrix elements.
  • A specific code structure is provided that includes a conditional statement to compare each matrix element to the current maximum value.

Areas of Agreement / Disagreement

Participants generally agree on the need to revise the original code to correctly implement the "while" loop and comparison logic, though there is no explicit consensus on the best approach as multiple suggestions are provided.

Contextual Notes

Limitations include the initial misunderstanding of variable usage and the need for a clear indexing strategy to access matrix elements. The discussion does not resolve all potential variations in implementation.

Who May Find This Useful

Readers interested in MATLAB programming, particularly those learning about loops and matrix operations, may find this discussion beneficial.

Dell
Messages
555
Reaction score
0
matlab command "while"

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


I'm sorry, but what is i doing here? I don't see how you are referencing it.
 


sorry, i meant to be n
 


You're somehwat close. Your function is setting the max to the value specified by a(n), which in this case is a(1,1). Your while loop, however, only works when the a(n) is < max. Earlier in the problem, you defined the max and a(n) to be equal, so the loop will not run.

What you want to do is index the entire matrix so you can check each value at each position, and then compare all of the values to find out which one is the largest value.

The first thing you want to do is find the number of elements in the matrix because this will tell you the number of indices. The numel() function does this, and you can also find it by multiplying the number of rows and columns together.

Then you need to set up the while loop to run from the first index to the last index, so you will want to use:

n = 1
while n <= number of elements in the matrix
n = n + 1
(conditional statement)
end

This will check each value in the matrix

The next thing you want to do is compare each value to one another using a conditional. You should have the conditional be run on if the value at a(n) is larger than the value set as the max value.
The code should look something like this:

if a(n) > max
max = a(n)
end

Putting all fo this together:

a = rand(5)
n = 1
max = a(n)
while n <= numel(a)
if a(n) > max
max = a(n)
end
n = n + 1
end

I hope this was what you were looking for!
 


perfect, thanks,
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K