False Output from find-function?

조회 수: 3 (최근 30일)
Rene
Rene 2020년 8월 30일
Heyho
I'm looking for maximum values of a table and am using
[rows, columns] = find(A==max(A));
to do so in the attached matrix.
It gets me this:
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
19 17
17 18
18 18
19 19
20 20
The matrix is 20x20 and the output has 21 values. :S
Also in my opinion the line...
19 17
...isn't true, cause I can't see a maximum there? This shouldn't be there at first place, should it?
Before and after this everything looks fine to me.
Why is this and how can I prevent that from happening?

채택된 답변

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 8월 30일
max(A) is a vector, not a number:
max(A)
ans =
Columns 1 through 15
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.9999 1.0000 1.0000 1.0000 1.0000
Columns 16 through 20
1.0000 0.7950 1.0000 1.0000 0.9998
The value at A(19,17) is the 0.795, which is not a maximum of the matrix but rather a maximum of the column.
if you want to get the maximum of the whole matrix you need this:
max(A(:))
ans =
1.0000
And when you compare, remember that float value comparison is everything but reliable, so you need to have a given tolerance.
In your code it would be something like this:
[rows, columns] = find( abs(A-max(A(:)))<1e-4 );
ans =
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 18
18 18
19 19
  댓글 수: 3
Rene
Rene 2020년 8월 31일
I'm sorry but it did not work out like I wanted it.
I tried it with another matrix:
A = [1 0.5 0.7;0.4 0.8 0.4;0.2 0.3 0.7];
It only gave me the value 1.
I then created a loop to get the maximum of each row
k = 1;
for i = 1 : size(A,1)
Max(k,1) = max(A(i,:));
k = k+1;
end
This gives me the maximums I need in the right order.
1
0.8
0.7
Now I want the location too. Like so:
%row col value
1 1 1
2 2 0.8
3 3 0.7
I tried the find again but as soon as there are multiple of the same value in the matrix it wont work like this:
k = 1;
for i = 1 : size(A,1)
Max(k,1) = max(A(i,:));
[row,col] = find(A==max(A(i,:)));
row(k,1) = row;
col(k,1) = col;
k = k+1;
end
Any idea how to get the column value of the exact max value I'm looking for?
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 9월 2일
This will probably work for you
A = [1 0.5 0.7;0.4 0.8 0.4;0.2 0.3 0.7];
Amax = max(A); % Get maximum from each column
DiffFromMax = A-Amax; % Zero values are maximum locations
tol = -1e-4; % Note that the tolerance is negative
[rows, columns] = find( DiffFromMax>tol )

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

추가 답변 (0개)

카테고리

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