简体   繁体   中英

Finding minimum elements of a matrix for each line - MATLAB

Here is the example:

I have the following matrix:

4 0 3
5 2 6
9 4 8

Now, i want to find two minimum values, and their indexes for each row. So the result is:

row1: 0 , position (1,2) and 3, position (1,3)
row2...
row3....

Well i am using lots of for cycles, and it is pretty complex. So is the any way of using MATLAB function to achieve my goal?

I have tried, but no result:

C=min(my_matrix,[],2)
[C(1),I] = MIN(my_matrix(1,:)) &find the position of the minimum value in row 1??

You can sort each row of your matrix in an ascending order, and then pick the first two indices for each row, like so:

[A_sorted, I] = sort(A, 2);
val = A_sorted(:, 1:2)
idx = I(:, 1:2)

Now val should contain the values of the first two smallest elements in each row, and idx should contain their column numbers.

If you want to print everything on the screen in a formatted fashion (as shown in your question), you can use the all-mighty fprintf command:

rows = (1:size(A, 1))';
fprintf('row %d: %d, position (%d, %d) and %d, position (%d, %d)\n', ...
    [rows - 1, val(:, 1), rows, idx(:, 1), val(:, 2), rows, idx(:, 2)]')

Example

A = [4, 0, 3; 5, 2, 6; 9, 4, 8];

%// Find two smallest values in each row and their positions
[A_sorted, I] = sort(A, 2);
val = A_sorted(:, 1:2)
idx = I(:, 1:2)

%// Print the result
rows = (1:size(A, 1))';
fprintf('row %d: %d, position (%d, %d) and %d, position (%d, %d)\n', ...
    [rows - 1, val(:, 1), rows, idx(:, 1), val(:, 2), rows, idx(:, 2)]')

The result is:

val =
     0     3
     2     5
     4     8

idx =
     2     3
     2     1
     2     3

and the formatted output is:

row 0: 0, position (1, 2) and 3, position (1, 3)
row 1: 2, position (2, 2) and 5, position (2, 1)
row 2: 4, position (3, 2) and 8, position (3, 3)

You can easily do this using sort .

[A_sorted, idx] = sort(A,2); % specify that you want to sort along rows instead of columns

The column of idx contains the minimum value for each row of A and the second column has the index of the second smallest value.

The minimum values can be retrieved from A_sorted

You can do something like below, where A is your matrix.

[min1, sub1] = min(A, [], 2);  % Gets the min element of each row
rowVec = [1:size(A,1)]';       % A column vector of row numbers to match sub1
ind1 = sub2ind(size(A), rowVec, sub1)  % Gets indices of matrix A where mins are
A2 = A;                        % Copy of A
A2(ind1) = NaN;                % Removes min elements of A
[min2, sub2] = min(A2, [], 2); % Gets your second min element of each row

min1 will be your vector of smallest values, min2 your vector of second smallest values for each row. Their respective indices for the row will be in sub1 and sub2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM