I want to find minimum values from an array?.
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Star Strider
on 11 Nov 2015
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 Comments
Star Strider
on 11 Nov 2015
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);
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!