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

조회 수: 6 (최근 30일)
Jingyu Liu
Jingyu Liu 2022년 4월 7일
댓글: Jingyu Liu 2022년 4월 7일
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개)

Walter Roberson
Walter Roberson 2022년 4월 7일
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.

카테고리

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