我们有一个M = [10 x 4 x 12]矩阵.作为例子,我采取M(:,:,4):
val(:,:,4) =
0 0 1 0
0 1 1 1
0 0 0 1
1 1 1 1
1 1 0 1
0 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到这个:
val(:,:,4) =
0 0 3 0
0 2 2 2
0 0 0 4
1 1 1 1
1 1 0 1
0 2 2 2
1 1 1 1
1 1 1 1
0 0 3 3
0 0 3 3
Run Code Online (Sandbox Code Playgroud)
注意:M构造逻辑矩阵:
Tab = [reshape(Avg_1step.',10,1,[]) reshape(Avg_2step.',10,1,[]) ...
reshape(Avg_4step.',10,1,[]) reshape(Avg_6step.',10,1,[])];
M = Tab>=repmat([20 40 60 80],10,1,size(Tab,3));
Run Code Online (Sandbox Code Playgroud)
这是一种非常简单的方法,适用于2D和3D矩阵.
%// Find the column index of the first element in each "slice".
[~, idx] = max(val,[],2);
%// Multiply the column index with each row of the initial matrix
bsxfun(@times, val, idx);
Run Code Online (Sandbox Code Playgroud)