简体   繁体   English

为每一行找到矩阵的最小元素 - MATLAB

[英]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? 那么使用MATLAB函数实现我的目标的方法是什么?

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. 现在val应该包含每行中前两个最小元素的值,而idx应该包含它们的列号。

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: 如果要以格式化方式在屏幕上打印所有内容(如问题所示),可以使用all-mighty fprintf命令:

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 . 您可以使用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. idx列包含A每行的最小值,第二列包含第二个最小值的索引。

The minimum values can be retrieved from A_sorted 可以从A_sorted检索A_sorted

You can do something like below, where A is your matrix. 你可以做类似下面的事情,其中A是你的矩阵。

[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. min1将是最小值的向量, min2是每行第二个最小值的向量。 Their respective indices for the row will be in sub1 and sub2 . 它们各自的行索引将在sub1sub2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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