How to create a 3 * 3 * 1 array using ones or zeros?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Stephen23
le 7 Avr 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."
Réponses (1)
Walter Roberson
le 7 Avr 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.
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!