필터 지우기
필터 지우기

find the non zero minimum and return the index

조회 수: 37 (최근 30일)
yu yue
yu yue 2016년 10월 21일
답변: Star Strider 2016년 10월 21일
I have a matrix
x = [12 10 0 6]
I need to find the non zero minimum and return the index of this non zero minimum, I have wrote
[A,I]=min(x(x>0))
It return that
A=6; I=3;
How can I get
A=6; I=4
? Thank you.

채택된 답변

Star Strider
Star Strider 2016년 10월 21일
If you want to temporarily change ‘x’, this works:
x = [12 10 0 6];
x(x == 0 ) = NaN;
[A,I] = min(x)
A =
6
I =
4
You can then change the NaN back to a zero if you want to:
x(isnan(x)) = 0
x =
12 10 0 6

추가 답변 (2개)

Image Analyst
Image Analyst 2016년 10월 21일
Try this:
% Setup
rows = 4; % Whatever you want.
columns = 4; % Whatever you want.
sigma1 = .5
m = sigma1*randn(rows, columns)
% Now begin:
% Set negative to inf
m(m<=0) = inf
minValue = min(m(:)) % Find min.
[rowOfMin, colOfMin] = find(m == minValue) % Find row and col of min.

Swarooph
Swarooph 2016년 10월 21일
One answer could be, set anything 0 or less to NaN and directly use the min function.
x = [12 10 0 6]
x1 = x; %Copy x into x1 if you don't want to lose x1
x1(x<=0) = NaN %Set anything <= 0 to NaN
[A,I]=min(x1) %Find min value and index
Now A = 6 and I = 4.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by