Minimum value in 2d matrix but with restriction

1 回表示 (過去 30 日間)
Matt Learner
Matt Learner 2012 年 3 月 29 日
I have a n*m matrix. for example A = [16 2 0 13; 5 11 10 8; 0 7 6 1; 16 14 15 0]. I want to find the min value>0 (i.e.,min value should not be 0, it can be any other value) in the matrix and also i need to find the index of matrix. I tried using [c,i] = min (A(A~=0)) and the answer I obtained is c = 1 (the min value as 1) and i = 13 (the index as 13). then when I used [d,e] = ind2sub(size(A),i) it gave me the answer as 1 and 4 i.e., index as (1,4) which is wrong. actually the index should be (3,4) because the index of 1 in matrix A is (3,4). Please help me in finding the correct index of the min value 1.

採用された回答

Thomas
Thomas 2012 年 3 月 29 日

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2012 年 3 月 29 日
x2 = x;
x2(~x) = inf;
[val idx] = min(x2)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by