How to create a 3 * 3 * 1 array using ones or zeros?
4 views (last 30 days)
Show older comments
The help of ones or zeros tell us "If any trailing dimensions greater than 2 have a size of 1, then the output, X, does not include those dimensions". But if I really need to create a 3 * 3 * 1 array (I care about the dimension), what can I do?
1 Comment
Stephen23
on 7 Apr 2022
All arrays implicitly have infinite trailing scalar dimensions, so your array already has 3rd dimension == 1.
"Arrays in MATLAB are N-dimensional, with an infinite number of trailing singleton dimensions."
Answers (1)
Walter Roberson
on 7 Apr 2022
A = zeros(3,3)
[size(A,1), size(A,2), size(A,3)]
so using size() says that A is length 1 on the third dimension.
If you needed size() of a numeric array to show 3 x 3 x 1, then you would need to provide your own size.m (and possibly ndims.m as well), but that would have a high risk of breaking something in MATLAB.
If the object does not need to be numeric, then you could create your own class that defined its own size() method to report back whatever you wanted. You could also define arithmetic operations on the class.
See Also
Categories
Find more on Loops and Conditional Statements 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!