How to expend 1 dim for array ?
Show older comments
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 Comment
"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)
Accepted Answer
More Answers (2)
Kanishk Singhal
on 12 Jul 2023
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 Comment
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
渲航
on 17 Jul 2023
0 votes
1 Comment
Walter Roberson
on 17 Jul 2023
isequal(size(Tensor, 1:3), [4 4 4]) & ndims(Tensor) <= 4
should work for the original arrangement
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!