MATLAB question:
I have an array A(2,2,2) that is three-dimensional. I would like to define a 2x2 array as a subarray of A, as follows:
B = A(1,:,:).
That is, we are simply projecting on the first component. But matlab will now treat this 2x2 matrix as a 1x2x2 array instead, so that I can't do certain things (like multiply by another 2x2 matrix).
How do I get B as a 2x2 subarray of A?
If you think about a skyscraper, your A(1,:,:)
is taking the first floor out and this operation inevitably happens across the 3rd dimension.
You can use reshape()
, squeeze()
or permute()
to get rid of the singleton dimension:
reshape(A(1,:,:),2,2)
squeeze(A(1,:,:))
permute(A(1,:,:),[2,3,1])
squeeze()
pretty much does all the job by itself, however it is not an inbuilt function and in fact uses reshape()
. The other two alternatives are expected to be faster.