how to find the position of in the matrix without using the neither find nor built in functions.

조회 수: 1 (최근 30일)
i have a matrix with the size of m x n . I found the min and max with the multiple for loops in the matrix without using any built in function . But i cannot find the position of them. Can you help me to find the position without using any built in function
  댓글 수: 7

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2020년 4월 16일
Just add variables to keep track of the locations. E.g.,
if M(a,b) < min
min = M(a,b);
mina = a;
minb = b;
end
if M(a,b) > max
max = M(a,b);
maxa = a;
maxb = b;
end
P.S. You should use variable names other than min and max, since those are existing MATLAB fuction names.
  댓글 수: 3
James Tursa
James Tursa 2020년 4월 16일
편집: James Tursa 2020년 4월 16일
You also need to change:
  • Initial max value from 0 to -inf
  • The < test to <=
  • The > test to >=
The last two changes are just to ensure that you always get the indexing results.
Walter Roberson
Walter Roberson 2020년 4월 16일
Unfortunately this code uses the built-in functions lt() and gt() and subsref()
In MATLAB, it is not possible to index into an array without using the built-in function subsref(), and that means that the overall task of finding the minimum and maximum of the array cannot be done without using any built-in functions.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Goodmanson
David Goodmanson 2020년 4월 16일
Hi Umut,
all you need do is use a couple of 1x2 arrays to store a and and b when you find them
When inside the for loops when you find a new max,
maxM = M(a,b);
indmax = [a b];
and similarly for min. I changed max to maxM because max is already a builit-in Matlab function and you will cause yourself problems if you use it as a variable name. Same for min.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by