How to create a 3 * 3 * 1 array using ones or zeros?

6 vues (au cours des 30 derniers jours)
Jingyu Liu
Jingyu Liu le 7 Avr 2022
Commenté : Jingyu Liu le 7 Avr 2022
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?

Réponses (1)

Walter Roberson
Walter Roberson le 7 Avr 2022
A = zeros(3,3)
A = 3×3
0 0 0 0 0 0 0 0 0
[size(A,1), size(A,2), size(A,3)]
ans = 1×3
3 3 1
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.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by