Effacer les filtres
Effacer les filtres

Finding index for minimum value in array

9 vues (au cours des 30 derniers jours)
Bathrinath
Bathrinath le 6 Mai 2014
Réponse apportée : Tarun le 3 Juil 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);

Réponse acceptée

Mischa Kim
Mischa Kim le 6 Mai 2014
Modifié(e) : Mischa Kim le 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);

Plus de réponses (1)

Tarun
Tarun le 3 Juil 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)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by