Happy new year SO members!
My first post for the new year, hoping to get some luck! :D
I have a 4D matrix X whose size is 100, 100, 100, 3. I extract its submatrix with X(51,:,51,:) and expect the result to be a 100x3 2D matrix. But NO, the result is a 4D matrix with size=1, 100, 1, 3. How come??
An even more confusing result is:
I can use plot(X(51,:,51,i)) and plot(X(:,51,51,i)) with i=1, 2, 3 just fine
can't use plot(X(51,51,:,i)) with the same i
in short, MATLAB submatrix extraction won't result in a size reduced matrix? and the different sub-access behaves differently, namely, more careful with the last dimension?
Thank you!
Edit01:
For convenience, I'd show my test results with singleton and squeeze here:

use of squeeze:

As you've noticed, when you index into your 100 by 100 by 100 by 3 matrix with (51, :, 51, :) you get back a result of size [1 100 1 3]. The dimensions with size = 1 are called singleton dimensions.
MATLAB won't remove non-trailing singleton dimensions automatically (for greater than 2D matrices) - use squeeze to eliminate these dimensions. If the last dimension is singleton, this is handled automatically.
So in your case, you have
X = zeros(100,100,100,3);
Y = X(51,:,51,:); #% size(Y) = [1 100 1 3];
Y2 = squeeze(X(51,:,51,:)); #% size(Y2) = [100 3] - singletons removed
P1 = X(51,:,51,1); #% size(P1) = [1 100 1 1] which becomes [1 100] (2D)
P2 = X(51,51,:,1); #% size(P2) = [1 1 100 1] which becomes [1 1 100] (3D)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With