Singleton dimention as last dimension in matrix
Show older comments
Hello,
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!
11 Comments
Bernard
on 23 Nov 2011
Jan
on 23 Nov 2011
You cannot create a [64x64x1] array in Matlab, as Fangjun, cyclist and Wong have explained (+1 for all).
If the function you want to use does not catch a trailing singelton dimension, and you do not want to modify this function, you cannot use it.
Fangjun Jiang
on 23 Nov 2011
If the function expects a 64x64x1 matrix, then a 64x64 matrix is right for it and it shouldn't cause syntax error or functionality error. For example, data(i,j,1) is the same as data(i,j). size(data,3) returns 1 for both. You have not presented a use case that proves otherwise. Take another look at your data, see if there is other reason that causes the incorrect result. For example, might it be the first dimension and second dimension are reversed?
Jan
on 23 Nov 2011
The called function can fail, if it uses "if ndim(In)~=3" or "s = size(In); dim3 = s(3)".
Bernard
on 28 Nov 2011
There is a way to create a dimension as [64 x 64 x 1]: In a Mex-function you can write in the dimensions vector. The official functions like mxSetDimensions care for the removing of trailing ones, but you can access the dimensions directly using undocumented methods. Anyhow, I strongly suggest not to do this. A bug in this code might have desasterous effects.
As Jan pointed out here, the function the OP was given probably used V = SIZE(X) and wrongly assumed something about the dimensions of interest based on the number of elements in V. The robust approach would be to call either
- N = size(X,D)
- [~,~,..,N,~] = size(X)
for each dimension of interest, both of which also work without error for trailing singleton dimensions.
the cyclist
on 29 May 2023
I nominate @Stephen23's comment for the Best Comment Posted More Than 10 Years After the Original Question Award.
Fangjun Jiang
on 27 Jun 2023
Or the best mind reader? I agree that is a good use case.
Erik Newton
on 28 Mar 2024
In my use case, I'm encoding to Json and sending to a remote (3rd-party non-matlab) system which has to parse it. It is expecting 3 dimensions. and so as an example of my problem:
>> jsonencode(zeros(3,2,2))
ans =
'[[[0,0],[0,0]],[[0,0],[0,0]],[[0,0],[0,0]]]'
>> jsonencode(zeros(3,2,1))
ans =
'[[0,0],[0,0],[0,0]]'
I wish/need to keep a consistent level of square brackets, independent of whether I just happen to only have a single case in the 3rd dimension.
@Erik Newton: here is a workaround (does not work for scalar nor empty inputs). Assumes you want a 3D array.
A = randi(9,3,2,1);
[R,C,~] = size(A);
T = jsonencode(cat(3,A,nan(R,C,1)));
T = strrep(T,',null]',']')
Accepted Answer
More Answers (3)
Fangjun Jiang
on 22 Nov 2011
Unless you have further process need, there is really no need to do that.
>> size(rand(10))
ans =
10 10
>> size(rand(10),3)
ans =
1
>> size(rand(10),5)
ans =
1
David
on 25 Oct 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
%filter function.
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');
1 Comment
David
on 25 Oct 2013
Nevermind... convn actually does add extra singleton dimensions as needed- the reason this wasn't working is that the 'same' option matches to the first matrix, not the second. So this modification works:
dBdX = convn(Bthree,reshape([-5 0 5],3,1,1),'same');
dBdY = convn(Bthree,reshape([-5 0 5],1,3,1),'same');
dBdZ = convn(Bthree,reshape([-5 0 5],1,1,3),'same');
Hin Kwan Wong
on 22 Nov 2011
3 votes
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=[5] has dimension 1 as well as 1x1 and 1x1x1 and 1x1x1x1...
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!