I want to find minimum values from an array?.

2 views (last 30 days)
Usman
Usman on 11 Nov 2015
Commented: Star Strider on 11 Nov 2015
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

Answers (1)

Star Strider
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
Usman
Usman on 11 Nov 2015
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 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);

Sign in to comment.

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!