使用多个条件分配值
Assign values w/ multiple conditions
让我们有一个 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
如何获得:
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
- 如果我在第一列中有 1,那么所有后续的 1 都应该是 1。
- 如果我在第一列中有 0 而在第二列中有 1,则所有后续的 1 都应该是 2。
- 如果我在第一列和第二列中有 0,但在第三列中有 1,那么所有后续的 1 都应该是 3。
- 如果我在前 3 列中有 0,但在第四列中有 1,那么这应该是四。
注:构造逻辑矩阵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));
这可能是一种方法 -
%// Concatenate input array along dim3 to create a 2D array for easy work ahead
M2d = reshape(permute(M,[1 3 2]),size(M,1)*size(M,3),[]);
%// Find matches for each case, index into each matching row and
%// elementwise multiply all elements with the corresponding multiplying
%// factor of 2 or 3 or 4 and thus obtain the desired output but as 2D array
%// NOTE: Case 1 would not change any value, so it was skipped.
case2m = all(bsxfun(@eq,M2d(:,1:2),[0 1]),2);
M2d(case2m,:) = bsxfun(@times,M2d(case2m,:),2);
case3m = all(bsxfun(@eq,M2d(:,1:3),[0 0 1]),2);
M2d(case3m,:) = bsxfun(@times,M2d(case3m,:),3);
case4m = all(bsxfun(@eq,M2d(:,1:4),[0 0 0 1]),2);
M2d(case4m,:) = bsxfun(@times,M2d(case4m,:),4);
%// Cut the 2D array thus obtained at every size(a,1) to give us back a 3D
%// array version of the expected values
Mout = permute(reshape(M2d,size(M,1),size(M,3),[]),[1 3 2])
使用随机 6 x 4 x 2
大小的输入数组编码 运行 -
M(:,:,1) =
1 1 0 1
1 0 1 1
1 0 0 1
0 0 1 1
1 0 0 0
1 0 1 1
M(:,:,2) =
0 1 0 1
1 1 0 0
1 1 0 0
0 0 1 1
0 0 0 1
0 0 1 0
Mout(:,:,1) =
1 1 0 1
1 0 1 1
1 0 0 1
0 0 3 3
1 0 0 0
1 0 1 1
Mout(:,:,2) =
0 2 0 2
1 1 0 0
1 1 0 0
0 0 3 3
0 0 0 4
0 0 3 0
这是一种非常简单的方法,适用于 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);
让我们有一个 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
如何获得:
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
- 如果我在第一列中有 1,那么所有后续的 1 都应该是 1。
- 如果我在第一列中有 0 而在第二列中有 1,则所有后续的 1 都应该是 2。
- 如果我在第一列和第二列中有 0,但在第三列中有 1,那么所有后续的 1 都应该是 3。
- 如果我在前 3 列中有 0,但在第四列中有 1,那么这应该是四。
注:构造逻辑矩阵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));
这可能是一种方法 -
%// Concatenate input array along dim3 to create a 2D array for easy work ahead
M2d = reshape(permute(M,[1 3 2]),size(M,1)*size(M,3),[]);
%// Find matches for each case, index into each matching row and
%// elementwise multiply all elements with the corresponding multiplying
%// factor of 2 or 3 or 4 and thus obtain the desired output but as 2D array
%// NOTE: Case 1 would not change any value, so it was skipped.
case2m = all(bsxfun(@eq,M2d(:,1:2),[0 1]),2);
M2d(case2m,:) = bsxfun(@times,M2d(case2m,:),2);
case3m = all(bsxfun(@eq,M2d(:,1:3),[0 0 1]),2);
M2d(case3m,:) = bsxfun(@times,M2d(case3m,:),3);
case4m = all(bsxfun(@eq,M2d(:,1:4),[0 0 0 1]),2);
M2d(case4m,:) = bsxfun(@times,M2d(case4m,:),4);
%// Cut the 2D array thus obtained at every size(a,1) to give us back a 3D
%// array version of the expected values
Mout = permute(reshape(M2d,size(M,1),size(M,3),[]),[1 3 2])
使用随机 6 x 4 x 2
大小的输入数组编码 运行 -
M(:,:,1) =
1 1 0 1
1 0 1 1
1 0 0 1
0 0 1 1
1 0 0 0
1 0 1 1
M(:,:,2) =
0 1 0 1
1 1 0 0
1 1 0 0
0 0 1 1
0 0 0 1
0 0 1 0
Mout(:,:,1) =
1 1 0 1
1 0 1 1
1 0 0 1
0 0 3 3
1 0 0 0
1 0 1 1
Mout(:,:,2) =
0 2 0 2
1 1 0 0
1 1 0 0
0 0 3 3
0 0 0 4
0 0 3 0
这是一种非常简单的方法,适用于 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);