How to find the maximum value from a matrix without using max syntax?

조회 수: 3 (최근 30일)
Kenny Gee
Kenny Gee 2022년 3월 1일
편집: Davide Masiello 2022년 3월 1일
I have to find a max value without using matlab built in max syntax.
The code I have that I want to change the max syntax to for loop is
[i j] = max(abs(A(y:n, y)));
I tried following this code I found from another question:
MaxValue = -Inf;
row = 0;
column = 0;
for y = 1:size(A, 1)
for y = 1:size(A, 2)
if A(y, y) > MaxValue
MaxValue = A(y:n, y);
row = y:n;
column = y;
end
end
end
but my code is not running. I don't know where I am going wrong.

답변 (3개)

David Hill
David Hill 2022년 3월 1일
s=sort(A);
maxValue=s(end);

Cris LaPierre
Cris LaPierre 2022년 3월 1일
I suspect one issue to fix is that you use the same variable name for both loops. The variable can only hold one value at a time, so the loops are definitely not behaving as you intended.
for y = 1:size(A, 1)
for y = 1:size(A, 2)
Use a different variable for each loop.
for y1 = 1:size(A, 1)
for y2 = 1:size(A, 2)

Davide Masiello
Davide Masiello 2022년 3월 1일
편집: Davide Masiello 2022년 3월 1일
I guess you could try something like this
A = randi(100,4,8);
for i = 1:size(A,1)
for j = 1:size(A,2)
if any(A(i,j)< A(:))
else
maxA = A(i,j);
row = i;
col = j;
end
end
end
disp(A)
disp(maxA)
disp([row col])

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by