I want to find minimum values from an array?.

2 次查看(过去 30 天)
Min(x) simply gives the most smaller value but i dont want this i want atleast 2 min values and by taking those values i have to calculate the distance. In attached image i have calculated the local minima values and its location. now what i have to do is MATLAB search the minimas array and then locate min values for me. Values for minimas and locs are: Minimas =
-86.5647
-80.3647
-81.3588
-106.9882
-77.0765
-77.8235
-92.2353
-106.2235
-115.3118
-98.3706
locs =
30
34
36
50
93
97
110
121
127
136

回答(1 个)

Star Strider
Star Strider 2015-11-11
I would use the sort function with two outputs:
[x_sort,idx] = sort(x);
and take the first two values of each output vector, since the default behaviour of sort is to go from lowest to highest.
  4 个评论
Usman
Usman 2015-11-11
ok thankyou strider. I got the idea. but i want MATLAB to extract those minimum values autmatically. so i dont have to look up what are those and then tell to extract.
Star Strider
Star Strider 2015-11-11
My pleasure.
It is doing this automatically. If you want to create a function that will only return the lowest two values in a vector, save this code separately as its own .m file as: min2.m:
function lowest_two = min2(x)
% MIN2 Returns the lowest two values in a vector as
% a 2x2 array with the values in the first column
% and their indices in the second column.
x = x(:); % Create Column Vector
[v,ix] = sort(x); % Sort Ascending
lowest_two = [v(1:2) ix(1:2)]; % Return Lowest Two Values And Their Indices
end
Then call it in your code as:
lowest2 = min2(x);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by