[英]Find the row/column of all maximum values in each row of a matrix
I want search indexes maximum value/s in each row. 我希望搜索索引每一行的最大值/秒。 If row has more than one maximum, so I want to save both indexes. 如果行有一个以上的最大值,那么我想保存两个索引。
For example: 例如:
X = [5 6 8
1 2 3
4 4 0];
And I need indexes 我需要索引
inds = [1 3
2 3
3 1
3 2];
I wanted to use function max
but this function only saves one index. 我想使用函数max
但是此函数仅保存一个索引。
You can use max
to compute the max for each row and then compare the elements in each row to it's row-wise max
using bsxfun
and eq
. 您可以使用max
来计算每一行的max,然后使用bsxfun
和eq
将每一行的元素与其按行的max
进行bsxfun
。 Then you can find the row/column positions of these maxima. 然后,您可以找到这些最大值的行/列位置。 We use a transpose in there ( .'
) to ensure that we get the ordering of the output that you expect. 我们在其中使用转置( .'
),以确保我们得到您期望的输出顺序。
[c,r] = find(bsxfun(@eq, d, max(d, [], 2)).')
output = [r,c];
Another way to do it, would be to use max
and repmat
. 另一种方法是使用max
和repmat
。 First you find the maximum of each row using 首先,您使用找出每一行的最大值
rowMaximum=max(X,[],2);
Then you replicate the maximum so that it has the same dimension as your input and compare it to the input 然后,您复制最大值,使其具有与输入相同的尺寸,并将其与输入进行比较
logicalMaximum=repmat(rowMaximum,1,size(X,2))==X;
And the last thing you wanna do is converting this logical array into your desired indexes 最后,您要做的就是将此逻辑数组转换为所需的索引
[columns,rows]=find(logicalMaximum);
result=[rows,columns];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.