1

我有一个矩阵如下。

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

如果我有与 a 相同的矩阵为零值,有什么办法可以用 1 标记最大位置?类似跟随。

octave:65> z
z =

ans(:,:,1) =

   0   1
   0   1

ans(:,:,2) =

   0   1
   1   0

ans(:,:,3) =

   1   0
   0   1

我更喜欢像 max 函数那样无维度的解决方案。

谢谢。

4

1 回答 1

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 ) )
于 2012-04-30T05:02:38.177 回答