필터 지우기
필터 지우기

Finding index for minimum value in array

조회 수: 9 (최근 30일)
Bathrinath
Bathrinath 2014년 5월 6일
답변: Tarun 2022년 7월 3일
I need to find the index for the minimum value in pbest other than '0'. I got the value using the following code but I have the nan value in pbest in loops it is giving errors. Is there any other way to find the min value other than zero and also to find its index.Suggest with some points.
nk=1;n=6; pbest=[9;9.5;8;0;0]; pbest(~pbest)=nan; p1best=min(pbest); [wt,wr]=min(pbest);

채택된 답변

Mischa Kim
Mischa Kim 2014년 5월 6일
편집: Mischa Kim 2014년 5월 6일
Bathrinath, use
[val,ind] = min(pbest(~ismember(pbest,0)));
or, the more general approach
val = min(pbest(~ismember(pbest,0)));
ind = find(val==pbest);

추가 답변 (1개)

Tarun
Tarun 2022년 7월 3일
If you only need the second output from a function, you can use a tilde (~) to ignore specific outputs.
For example, you might only want the index containing the maximum value in a vector:density = data(:,2)
[~,ivMax] = max(v2)
densityMax = density(ivMax)
Try getting the index value of the minimum value in v2. Use this index to extract from density.
data
density=data(:,2)
[~,ivMax]=max(v2)
densityMax=density(ivMax)
density=data(:,2)
[~,ivMin]=min(v2)
densityMin=density(ivMin)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by