How to expend 1 dim for array ?

조회 수: 40 (최근 30일)
渲航
渲航 2023년 7월 12일
댓글: Walter Roberson 2023년 7월 17일
I already have a vector s , which size is
Now want to change its size to for another function that normally has an input size of 4x4x4xn (n >=1). I tried this command:
s_expend(:,:,:,1) = s
but I found that the size of s_expend is still (Use command: size(s_expend))
It seems that MATLAB ignores dimensions of size 1 that are the last dimension? How do I implement something like expend_dim in numpy or unsqueeze in Pytorch using Matlab?
  댓글 수: 1
Stephen23
Stephen23 2023년 7월 12일
"It seems that MATLAB ignores dimensions of size 1 that are the last dimension?"
Not at all. If you check that dimension, you will find that it has size 1 (as do all infinite trailing dimensions):
A = rand(4,4,4);
size(A,4)
ans = 1

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2023년 7월 12일
이동: Matt J 2023년 7월 12일
MATLAB does not exactly ignore trailing dimension of 1, but thinking of it that way is "close enough" for most work.
Every array in MATLAB is treated as having an indefinite number of trailing dimensions of size 1. A 4 x 4 x 4 array is treated the same as a 4 x 4 x 4 x 1 array or a 4 x 4 x 4 x 1 x 1 x 1 x 1 x 1 x 1 array.
When MATLAB is storing the dimensions of an array internally, all singular (length 1) trailing dimensions are omitted from being explicitly stored, and the number of stored dimensions (ndims) for any array is always the minimum of 2 and "the last dimension number that is not 1". Trailing dimensions that are size 1 are "collapsed".
If you look at an array and it is 4 x 4 x 4, ndims 3, and you need to check whether it is compatible with a 4 x 4 x 4 x n array, then when it makes sense to do so, MATLAB automatically internally replicates data to match. If you were, for example, adding a 4 x 4 x 4 to a 4 x 4 x 4 x 7 then MATLAB would do the internal equivalent of repmat(The3DArray, 1, 1, 1, 7) and add that to the 4 x 4 x 4 x 7. If you were doing cat(4, The3DArray, The4DArray) then that would work without difficulty and would give you a 4D array .
There are cases where you do need exactly the same size of array; in such a case you can explicitly test isequal(size(TheFirstArray), size(TheSecondArray)) . But if you are checking for arithematic compatibility then ndims(TheFirstArray) <= ndims(TheSecondArray)

추가 답변 (2개)

Kanishk Singhal
Kanishk Singhal 2023년 7월 12일
Yeah, as you said MATLAB ignnores dimension of size 1, but if you do,
A = [1 2;3 4];
size(A,3)
You'll get 1 which I think is what you want. If you want to loop in the function you can use size to find the dimension you need.
Hope it helps.
  댓글 수: 1
Steven Lord
Steven Lord 2023년 7월 12일
In release R2019b we enhanced the size function to accept a vector of values for the dimension input. So if that's all the data you need, no loop is required.
A = ones(4, 5, 6);
sz = size(A, 1:10) % Size of A in dimensions 1 through 10
sz = 1×10
4 5 6 1 1 1 1 1 1 1

댓글을 달려면 로그인하십시오.


渲航
渲航 2023년 7월 17일
Thanks for all your kind advice. The function I metioned actually accepts a Tensor to run so only checking the size of the tensor is not enough (the size mismatch will get an error).
The best solution for me is to change the tensor size from the to in the function param, in this case even still has the same size.
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 7월 17일
isequal(size(Tensor, 1:3), [4 4 4]) & ndims(Tensor) <= 4
should work for the original arrangement

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by