필터 지우기
필터 지우기

Find n minimum values in an array?

조회 수: 98 (최근 30일)
ME
ME 2015년 4월 15일
댓글: Chris Volpe 2023년 2월 1일
I have an array, I need to be able to select 2, or 4 or so on 'n' minimum (smallest) values from the specific array? I know i can use 'min' function but this only gives one smallest value. I do not want to sort the array in order and pick values. is there another way ?????
  댓글 수: 3
Margarida Costa
Margarida Costa 2017년 1월 22일
And is there an easy way to get back the indexes of those values?
Nicholas Ayres
Nicholas Ayres 2020년 8월 21일
The "optional second output" of sort is the index order.
[sortedVals,indexes] = sort(myvals);
So
myVals = [1 0.1 7 10 4];
[sortedVals,indexes] = sort(myVals);
gives
sortedVals = [0.1 1 4 7 10]
indexes = [2 1 5 3 4]
Figured I'd pop this here in case anyone else comes along with the same question. :)

댓글을 달려면 로그인하십시오.

채택된 답변

Titus Edelhofer
Titus Edelhofer 2015년 4월 15일
Hi,
if you don't want to sort, and n is not too large, you could remove the index, something like
x = rand(1000, 1);
n = 5;
val = zeros(n,1);
for i=1:n
[val(i),idx] = min(x);
% remove for the next iteration the last smallest value:
x(idx) = [];
end
Titus
  댓글 수: 5
Rik
Rik 2019년 7월 8일
I know this is an old thread, but still:
Even replacing x(idx) with NaN instead of removing the entry doesn't really improve the speed. This might be dependent on the release and of the size of the input array.
zaid tahir
zaid tahir 2021년 3월 5일
@Titus Edelhoferthanks for the code. Helped make a very complex problem, very simple.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Jan
Jan 2017년 1월 23일
While sorting is the best approach to get the data, you can easily obtain an unsorted result:
n = 2;
[xs, index] = sort(x);
result = x(sort(index(1:n)))
  댓글 수: 1
Chris Volpe
Chris Volpe 2023년 2월 1일
Seems to me you want to leave out the "sort" call in line 3:
result = x(index(1:n))

댓글을 달려면 로그인하십시오.


Salam Ismaeel
Salam Ismaeel 2018년 8월 30일
use: mink() command for R2018a
https://www.mathworks.com/help/matlab/ref/mink.html
  댓글 수: 3
Bill Tubbs
Bill Tubbs 2020년 6월 22일
This is the correct answer. Thanks!
Example:
>> X = [4 1 2 1 5 4 2];
>> [minValues, idx] = mink(X,3)
minValues =
1 1 2
idx =
2 4 3
Martin Vankát
Martin Vankát 2020년 10월 25일
Thank you! This one helped.

댓글을 달려면 로그인하십시오.


Titus Edelhofer
Titus Edelhofer 2015년 4월 15일
편집: Titus Edelhofer 2015년 4월 15일
Hi,
if it's not floating point but integers (so no problem with roundoff),you could do the following:
x = [1 3 2 4 1 3 5 4 1 3 1]
% find the first 2 min occurrences:
idxMin = find(x==min(x), 2, 'first')
This tells that the smallest value 1 is found at positions 1 and 5. Or are you looking for the n smallest values in the sense, you are looking for the smallest, the second smallest etc.? In this case the answer is simply
n = 2;
xs = sort(x);
% pick the n smallest:
xs(1:n)
Titus
  댓글 수: 5
Titus Edelhofer
Titus Edelhofer 2015년 4월 15일
If this was what you were looking for, then please mark the question as answered ... Thanks, Titus
ME
ME 2015년 4월 15일
not entirely, because i don't want to sort the array.

댓글을 달려면 로그인하십시오.


Luis Gomez
Luis Gomez 2022년 8월 30일
편집: Luis Gomez 2022년 8월 30일
I do it like this,
% k miminum values of a matrix (return the position, not the value)
% not optimized: it just worked for my application.
a = magic(5)
a = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
%Copy the original data
aa = a;
%Size of the matrix
[rows columns] = size(a);
%Select the k mininum values
k = 3;
k_mins = zeros(k,1); %to store solution (not really needed)
for i = 1: k
[m, I] = min(a(:));
a(I) = inf;
k_mins(i) = I;
end
k_mins
k_mins = 3×1
11 20 24
% to get the [i, j] usual matrix locations for ecah value within k_mins
% for instance, to get the "i,j" indices por first element of k_mins
% vector,
[i j] = ind2sub([rows, columns],k_mins(1))
i = 1
j = 3
%the value
aa(i,j)
ans = 1
  댓글 수: 8
Walter Roberson
Walter Roberson 2022년 8월 30일
Why not mink()?
Rik
Rik 2022년 8월 30일
@Walter, to be honest I always forget that function exists ever since it was introduced in R2017b.

댓글을 달려면 로그인하십시오.

카테고리

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