0

I currently have a 4 * 5057 matrix in MATLAB called "cols". What I'm trying to do is find the k minimum values of the first row, and store them in a new matrix called "mins".

So, say k was 3, I'd like the function to scan the entire first row of "cols", pick out the smallest 3 values, and copy that entire column to the new matrix.

I understand how to do the end part, but it's just designating how to actually write the function that finds not just the single lowest number, but multiple that stumps me.

I've read through the MathsWorks page on the min function as well as a few others that came up on a search, but with no luck.

Any help would be greatly appreciated! Please and thank you!

Kadin
  • 160
  • 2
  • 5
  • 17
  • I need to find the k minimum values for the first row, then I will use the values underneath them (in the same column). – Kadin May 05 '13 at 08:37

2 Answers2

0

The easiest method to get the k highest or lowest values from a vector is to first sort and then just take the first (or last) k values.

This is not the most efficient algorithm, but it is probably quick enough for your purposes.

I currently have a 4 * 5057 matrix in MATLAB called "cols". What I'm trying to do is find the k minimum values of the first row, and store them in a new matrix called "mins".

Try this:

v = cols(1, :);   % Get the first row of "cols"
v = sort(v);      % sort it    
mins = v(1:k);    % take the first k values

If I did sort the values, would that only affect the top row of the matrix? Because I need the values underneath the minimum values as well, so mixing them up isn't really preferable :/

Try this:

mins = sortrows(cols.', 1).'
mins = mins(:, 1:k)

Or this:

v = cols(1, :);         % Get the first row of "cols"
[v, ix] = sort(v);      % sort it, and remember the permutation
mins = cols(:, ix(1:k))
nibot
  • 14,428
  • 8
  • 54
  • 58
  • If I did sort the values, would that only affect the top row of the matrix? Because I need the values underneath the minimum values as well, so mixing them up isn't really preferable :/ – Kadin May 05 '13 at 08:36
  • Ah, I missed the part about "entire column". Added some code to the answer that should address this. – nibot May 05 '13 at 08:50
0

In case you need to keep the order of columns consistent, sortrows can do the job. Since sortrows operates on rows, we have to transpose twice. Extending the answer of @nibot, we have

   col_sorted = sortrows(cols')';
   k_min = col_sorted(:,1:k);

e.g.

   cols =  
           0.8147    0.9134    0.2785    0.9649
           0.9058    0.6324    0.5469    0.1576
           0.1270    0.0975    0.9575    0.9706

   sortrows(cols')' = 
           0.2785    0.8147    0.9134    0.9649
           0.5469    0.9058    0.6324    0.1576
           0.9575    0.1270    0.0975    0.9706

then, you just have to extract the first k-columns.

Acorbe
  • 8,367
  • 5
  • 37
  • 66
  • I had literally just stumbled across that! :D I used a code pretty much exactly the same. Thanks very much :) – Kadin May 05 '13 at 08:49