I want to find minimum values from an array?.

조회 수: 2 (최근 30일)
Usman
Usman 2015년 11월 11일
댓글: Star Strider 2015년 11월 11일
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에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by