필터 지우기
필터 지우기

Finding the second smallest value in each column of an array?

조회 수: 3 (최근 30일)
Jason
Jason 2011년 8월 7일
This is probably very simple. I have an array of size, say 10 by 30. In each column, there is definitely an element with a value of 0, the rest of the nine: values > 0. Now, I want a row vector containing the second smallest values from each column of the original array.
Any idea? If I were to use min(array), I would get a row vector with all 0's. Maybe I have to use bubble sorting?
  댓글 수: 1
Jason
Jason 2011년 8월 10일
All good answers. I am finding it hard to pick and accept one. Someone help me?

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

답변 (3개)

Jan
Jan 2011년 8월 7일
Sorting is a good idea:
X = rand(10, 10);
Xs = sort(X);
min2nd = Xs(2, :);
This is not very efficient, but most likely the fastest Matlab method.

Walter Roberson
Walter Roberson 2011년 8월 7일
t = array;
t(t==0) = inf;
min(t)

Pierre
Pierre 2011년 8월 7일
You could simply take advantage of Bruno Luong's Min/Max selection implemented in MEX. AFAIK they implement Hoare's selection algorithm (which is a deviation of quicksort).

카테고리

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