Finding index for minimum value in array
16 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
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
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)
0 个评论
另请参阅
类别
在 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!