I've a Matrix f.ex. like
A=[0,1,1,0; 1,0,0,1;0,0,0,1;0,1,1,1]; A =
0 1 1 0 1 0 0 1 0 0 0 1 0 1 1 1
only in 3D. So for example a Matrix like this only 100times (size(A)=4 x 4 x 100). Of course different shape/form of the '1s' and '0s'.
I want to as a result only the first '1' of each row and all other elements or further "1" to zero. The result should be:
B=[0,1,0,0;1,0,0,0;0,0,0,1;0,1,0,0];
B =
0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0
again, a hundred times. So the Result would be again size(B)= 4 x 4x 100.
If it was a 2D-matrix I'd use this: [Y,I] = max(A, [], 2); B = zeros(size(A)); B(sub2ind(size(A), 1:length(I), I')) = Y;
But that doesn't work for a 3Dim-Matrix…And I don't want to use for loop. Thanks!
Best Answer