Filter löschen
Filter löschen

Finding index for minimum value in array

9 Ansichten (letzte 30 Tage)
Bathrinath
Bathrinath am 6 Mai 2014
Beantwortet: Tarun am 3 Jul. 2022
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);

Akzeptierte Antwort

Mischa Kim
Mischa Kim am 6 Mai 2014
Bearbeitet: Mischa Kim am 6 Mai 2014
Bathrinath, use
[val,ind] = min(pbest(~ismember(pbest,0)));
or, the more general approach
val = min(pbest(~ismember(pbest,0)));
ind = find(val==pbest);

Weitere Antworten (1)

Tarun
Tarun am 3 Jul. 2022
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)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by