Say I have two 3D matrices of same dimension (3x3x2):
A =
ans(:,:,1) =
0 0 0
0 0 0
0 0 0
ans(:,:,2) =
0 0 0
0 0 0
0 0 0
B =
ans(:,:,1) =
0 1 0
0 1 0
0 1 0
ans(:,:,2) =
1 0 0
1 0 0
0 0 1
I would like to index the last page (third dimension) of A where the corresponding page of B equals 1 and turn those values into 2, so that A becomes:
A =
ans(:,:,1) =
0 0 0
0 0 0
0 0 0
ans(:,:,2) =
2 0 0
2 0 0
0 0 2
How can I do it? Is there an easy way?
This could be one approach to set the elements in last page in the third dimension of A
at places where the last page in the third dimension of B
have 1
's -
A(find(B(:,:,end)==1) + numel(B) - numel(B(:,:,1))) = 2
Sample run -
%// Starting input, A
A(:,:,1) =
0.2187 0.1097 0.4046 0.3658
0.1058 0.0636 0.4484 0.7635
A(:,:,2) =
0.6279 0.9329 0.1920 0.6963
0.7720 0.9727 0.1389 0.0938
A(:,:,3) =
0.5254 0.8611 0.3935 0.7413
0.5303 0.4849 0.6714 0.5201
%// Input, B
B(:,:,1) =
2 2 1 1
1 1 3 2
B(:,:,2) =
3 3 3 2
2 2 3 1
B(:,:,3) =
1 1 3 3
2 1 2 2
%// Output, A
A(:,:,1) =
0.2187 0.1097 0.4046 0.3658
0.1058 0.0636 0.4484 0.7635
A(:,:,2) =
0.6279 0.9329 0.1920 0.6963
0.7720 0.9727 0.1389 0.0938
A(:,:,3) =
2.0000 2.0000 0.3935 0.7413
0.5303 2.0000 0.6714 0.5201
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.