I want to find minimum values from an array?.

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

Réponses (1)

Star Strider
Star Strider le 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 commentaires

Usman
Usman le 11 Nov 2015
Modifié(e) : Usman le 11 Nov 2015
can you please tell me how this actually works? as i am new to matlab so have no idea? because [x_sort,idx] = sort(x); arrange the values in descending order eg: Minimas=[5 7 6 9] then after execution it becomes sort_x = [5 6 7 9] and my scenraion is i have to extract 5 and 7 as min value? any suggestion
Since you want only the first two values and ‘x_sort’ and ‘idx’ are column vectors:
Result = [x_sort(1:2) idx(1:2)]
will contain the two lowest values and their indices in the vector.
If they are row vectors:
Result = [x_sort(1:2); idx(1:2)]'
Usman
Usman le 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.
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);

Connectez-vous pour commenter.

Catégories

Question posée :

le 11 Nov 2015

Commenté :

le 11 Nov 2015

Community Treasure Hunt

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

Start Hunting!

Translated by