필터 지우기
필터 지우기

how to find maximum and minimum in a matrix

조회 수: 1 (최근 30일)
Maryam Hamrahi
Maryam Hamrahi 2016년 8월 4일
댓글: Maryam Hamrahi 2016년 8월 4일
I have matrix A with n rows and 2 columns. I would like to find rows which has the maximum and/or minimum of X and/or Y. Then, replace zero in these rows.
For instance: I have matrix A and I would like to produce matrix B.
A= [0.1 0.5
0.3 0.9
0.4 0.8
0.4 0.2
1 0.7
0.2 0.6
0.7 1
0.2 0.9]
Here, max. X=1 min. X=0.1 max. Y=1 min. Y=0.2
B= [0 0
0.3 0.9
0.4 0.8
0 0
0 0
0.2 0.6
0 0
0.2 0.9]

채택된 답변

Thorsten
Thorsten 2016년 8월 4일
A= [0.1 0.5
0.3 0.9
0.4 0.8
0.4 0.2
1 0.7
0.2 0.6
0.7 1
0.2 0.9]
idx = A(:,1)==max(A(:,1)) | A(:,1)==min(A(:,1)) |...
A(:,2)==max(A(:,2)) | A(:,2)==min(A(:,2));
A(any(idx,2),:) = 0
  댓글 수: 1
Maryam Hamrahi
Maryam Hamrahi 2016년 8월 4일
I really appreciate your help Thorsten. Thank you very much.

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

추가 답변 (1개)

Stephen23
Stephen23 2016년 8월 4일
편집: Stephen23 2016년 8월 4일
The simplest way would be to use logical indexing:
>> idx = A==max(A(:)) | A==min(A(:));
>> A(any(idx,2),:) = 0
A =
0.0 0.0
0.3 0.9
0.4 0.8
0.0 0.0
0.0 0.0
0.2 0.6
0.0 0.0
0.2 0.9
  댓글 수: 3
Stephen23
Stephen23 2016년 8월 4일
편집: Stephen23 2016년 8월 4일
@Maryam Hamrahi: you are right, I made a mistake, and have now corrected my answer. It should work for you now.
Maryam Hamrahi
Maryam Hamrahi 2016년 8월 4일
Thank you very much Stephen Cobeldick.
your code is working, but it gives me the wrong answer.
I want to have the following:
A =
0.0 0.0
0.3 0.9
0.4 0.8
0.0 0.0
0.0 0.0
0.2 0.6
0.0 0.0
0.2 0.9
but it gives me this:
A =
0 0
0.3000 0.9000
0.4000 0.8000
0.4000 0.2000
0 0
0.2000 0.6000
0 0
0.2000 0.9000

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by