Singleton dimention as last dimension in matrix
133 visualizaciones (últimos 30 días)
Bernard el 22 de Nov. de 2011
How do I create a singleton dimension as a last dimension in matlab, for example, so that size = 64 64 1.
I've tried reshape(x,[64 64 1]), but the resultant matrix is 64x64, not 64x64x1. Similarly with permute.
Thanks for any help!
the cyclist el 22 de Nov. de 2011
Arrays in MATLAB have an implied infinitely long series of trailing singleton dimensions. You can index into them with no problem. For example
>> x = rand(3,3);
is a valid indexing into x.
What is it that you are trying to do, that you need to emphasize that the array is 64x64x1?
Más respuestas (3)
Fangjun Jiang el 22 de Nov. de 2011
Unless you have further process need, there is really no need to do that.
David el 25 de Oct. de 2013
Trailing singleton dimensions ARE useful, and I want them too. This is useful for passing arguments to functions like convn. I would like to calculate discrete derivatives of a 3D data set as shown here. The calculation fails because reshape passes the simple partial derivative filter as 3x1 instead of 3x1x1. I can get around this by making the filter 3x3x3 and padding zeros, but its less clean:
%Bthree is 121,121,161 3D array
%Now we need to get the derivatives. I will try to convolve a simple linear
dBdX = convn(reshape([-5 0 5],3,1,1),Bthree,'same');
dBdY = convn(reshape([-5 0 5],1,3,1),Bthree,'same');
dBdZ = convn(reshape([-5 0 5],1,1,3),Bthree,'same');
Hin Kwan Wong el 22 de Nov. de 2011
64 x 64 x 1 with all due consideration is identical to 64 x 64...
You with see why with zeros(5,5,1), zeros(5,5,2) and zeros(5,5)
It's same as saying data= has dimension 1 as well as 1x1 and 1x1x1 and 1x1x1x1...