Index = Index+ Column -1

Hello All,
I am trying to figure out why does the formula 'Index=Index+Column-1' give a correct answer while finding the index of a maximum value in any matrix. For example:
A=[1 2 3; 40 10 13;17 30 31]
[n,n]=size(A);
for k=1:n-1
[maxV,I]=max(A(k:n,k));
I=I+k-1;
maxV
I
end
I won't get a right answer without using this formula. Can someone please help me know where is this formula 'I=I+k-1' derived from?
Dev

답변 (1개)

Roger Stafford
Roger Stafford 2015년 3월 8일

0 개 추천

Where you write
[maxV,I]=max(A(k:n,k));
the index, 'I', which is returned, is relative to the portion of A that is being presented to 'max'. All 'max' sees when 'k' is equal to 2, is the second and third rows of the second column of A. Naturally it gives 'I' a value of 1 since the maximum occurs in the first row it sees. The code adds a correcting "k-1" to compensate.
This code snippet does not find the maximum values in the columns of A. It checks only its first two columns, and in the second column only the bottom two values.

이 질문은 마감되었습니다.

질문:

2015년 3월 7일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by