finding the second and the third smallest value in a cell array

조회 수: 18 (최근 30일)
AA
AA 2014년 12월 24일
댓글: AA 2014년 12월 24일
A = num2cell(reshape(randperm(12)-6,[],2),2);
mn = cellfun(@(x) min(x(x>0)),A,'Un',0);
mn = min([mn{:}]) % Show the minimum positve value.
L = cellfun(@(x) find(x==mn),A,'Un',0);
idx = find(~cellfun('isempty',L),1,'last') % Which cell has the min.
L = L{idx} % And the positions
mn = min([mn{:}]) How can I rewrite this line so I get the second smallest value and third smallest value in that table of mins.
thanks

채택된 답변

Guillaume
Guillaume 2014년 12월 24일
편집: Guillaume 2014년 12월 24일
Instead of getting the min, simply sort the array and get the first three elements:
sortedmins = sort(cell2mat(mn)); %or sort([mn{:}]);
firsthreemins = sortedmins(1:3);
If you want these three minimum to be different, then use unique instead of sort.
  댓글 수: 1
AA
AA 2014년 12월 24일
or just that line mn1 = cellfun(@(x) min(x(x>mn)),A,'Un',0);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by