MATLAB: Finding the second and the third smallest value in a cell array

cell array min value

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

Best Answer

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.