我有一个矩阵如下。
octave:63> a
a =
ans(:,:,1) =
0.411710 0.947670
0.068291 0.368340
ans(:,:,2) =
0.27178 0.56699
0.54317 0.27393
ans(:,:,3) =
0.72621 0.44131
0.22743 0.61914
使用max函数,我可以根据特定维度获得最大值的索引。
octave:64> [a2_val a2_indx] = max(a, [], 2)
a2_indx =
ans(:,:,1) =
2
2
ans(:,:,2) =
2
1
ans(:,:,3) =
1
2
如果我有一个与零值相同的矩阵,有什么方法可以用1标记最大位置?像跟随一样。
octave:65> z
z =
ans(:,:,1) =
0 1
0 1
ans(:,:,2) =
0 1
1 0
ans(:,:,3) =
1 0
0 1
我更喜欢解决方案是无尺寸的,就像最大功能一样。
感谢。
答案 0 :(得分:1)
最简单的方法可能是沿着该维度复制最大矩阵并使用逻辑比较:
dim = 2; % The dimension along which we max
% Prep a size matrix for the replication
dims = ones( size( size( a ) ) );
dims( dim ) = size( a, dim ); % (all ones except the
% dimension that gets maxed)
result = ( a == repmat( max( a, [], dim ), dims ) )