minimun value in a column matrix with its index
2 次查看(过去 30 天)
显示 更早的评论
hi, I write the bellow code to find the minimum value in a column vector but it should not consider zero as a minimum , but how to find the exact indexing of the minimum value?
in the example the row index of minimum value exept zero should be 2 , why it give me 1?
can anybody correct it for me so the row_idx = 2 !
A=[0; 5 ;6 ;9 ;55]
[min_value,row_idx]=min(A(A~=0))
0 个评论
采纳的回答
Dyuman Joshi
2022-6-10
编辑:Dyuman Joshi
2022-6-10
A=[0; 5 ;6 ;9 ;55]
A(A~=0) %when you do this, your vector changes
Method 1
minvalue=min(A(A~=0))
rowidx=find(A==minvalue)
Method 2
A(A==0)=nan
[min_value, row_idx]=min(A)
更多回答(1 个)
Voss
2022-6-10
A(A~=0) is a 4-by-1 vector:
A=[0; 5 ;6 ;9 ;55];
A(A~=0)
When you do min on that you find that 5 is the minimum, which occurs at index 1, as you can see right there.
To get the correct index in A (not in A(A~=0)) of the non-zero minimum, you can store the indices of the non-zero elements of A:
nz_idx = find(A~=0)
then use min:
[min_value,temp_idx] = min(A(nz_idx))
and then get the index in A:
row_idx = nz_idx(temp_idx)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!