Finding index for minimum value in array

17 ビュー (過去 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 件のコメント
Bathrinath
Bathrinath 2014 年 5 月 6 日
thank you sir

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by