【发布时间】:2014-05-27 10:56:41
【问题描述】:
我有一个这样的矩阵:
0.1 0.2 0.5
0.3 0.7 0.4
我知道我可以使用max 函数来查找每一行中的最大值。如何使用从max 返回的索引来创建一个新矩阵,如下所示:
0 0 1
0 1 0
【问题讨论】:
标签: arrays matlab matrix indexing rows
我有一个这样的矩阵:
0.1 0.2 0.5
0.3 0.7 0.4
我知道我可以使用max 函数来查找每一行中的最大值。如何使用从max 返回的索引来创建一个新矩阵,如下所示:
0 0 1
0 1 0
【问题讨论】:
标签: arrays matlab matrix indexing rows
result = bsxfun(@eq, A, max(A,[],2));
【讨论】:
代码
%%// Given matrix
A= [0.1 0.2 0.5;0.3 0.7 0.4]
%%// Get the column indices of max values across each row into y1
[~,y1] = max(A,[],2);
%%// Create a zero matix of size same as A and set the values corresponding
%%// to y1 along each row as 1
A1 = zeros(size(A));
A1(sub2ind(size(A1),1:numel(y1),y1'))=1
输出
A =
0.1000 0.2000 0.5000
0.3000 0.7000 0.4000
A1 =
0 0 1
0 1 0
【讨论】: