How can I find the n smallest numbers in a two-dimensional array of m numbers?

조회 수: 1 (최근 30일)
For example, with this 2x5 matrix (m=10 and n=4), the expected answer is
(2,9), (1,5), (1,3), (2,10)
111 , 112, 145, 212
236 245 145 244 112
225 232 266 111 212

채택된 답변

Iman Ansari
Iman Ansari 2013년 4월 13일
Hi.
n=4;
a=[236 245 145 244 112; 225 232 266 111 212];
[r c]=size(a);
a = a';
vector=a(:);
[B,IX] = sort(vector);
[fix((IX(1:n)-1)./c)+1 IX(1:n)]
  댓글 수: 2
Iman Ansari
Iman Ansari 2013년 4월 13일
Finding 4 Smallest values of matrix, but results in which rows and indexing in this order:
1 2 3 4 5
6 7 8 9 10

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by