MATLAB Answers

Bernard
1

Singleton dimention as last dimension in matrix

Bernard
さんによって質問されました 2011 年 11 月 22 日
最新アクティビティ David
さんによって コメントされました 2013 年 10 月 25 日
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!

  6 件のコメント

Jan
2011 年 11 月 23 日
The called function can fail, if it uses "if ndim(In)~=3" or "s = size(In); dim3 = s(3)".
Bernard
2011 年 11 月 28 日
Thanks for your help everyone (and sorry for the slow update), I've just written my own function instead of using the pre-existing one - the purpose of the function is to write >3 dimension data in a certain file format: it expects a 3D matrix as input, adds dimensions to it, then writes the file. In the old function, if a 2D matrix is input, then the number and order of the dimension written to the output file is wrong. Anyway, in the end I used my own function.
Thanks for you help, just wanted to absolutely make sure there's no way to have a singleton dimension at the end of a matrix!
Jan
2011 年 11 月 28 日
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.

サインイン to comment.

4 件の回答

the cyclist
回答者: the cyclist
2011 年 11 月 22 日
 採用された回答

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);
>> x(2,3,1,1,1,1,1,1,1,1,1,1,1)
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?

  0 件のコメント

サインイン to comment.


Fangjun Jiang
回答者: Fangjun Jiang
2011 年 11 月 22 日

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

  0 件のコメント

サインイン to comment.


回答者: Hin Kwan Wong 2011 年 11 月 22 日

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...

  0 件のコメント

サインイン to comment.


David
回答者: David
2013 年 10 月 25 日

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 件のコメント

David
2013 年 10 月 25 日
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');

サインイン to comment.



Translated by