Generate n-dimensional array: problem with 1D
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi all,
Imagine I'd like to generate random dimensional zeros matrix, I can do:
K>> zeros(2, 2)
ans =
0 0
0 0
K>> zeros(2, 2, 2)
ans(:,:,1) =
0 0
0 0
ans(:,:,2) =
0 0
0 0
and so on. However, for 1D case, I'll have to use:
K>> zeros(2, 1)
ans =
0
0
I cannot use zeros(2) directly, meaning I have to deal with 1D case separately. Any idea how to make it smarter?
댓글 수: 0
채택된 답변
Star Strider
2017년 11월 8일
That is as ‘smart’ as it gets, since it cannot read your mind. If you already have a vector and you want a zeros vector to match it, you can use the size of the original vector (or any dimension array) to create it:
x = 1:2;
v = zeros(size(x));
댓글 수: 2
Star Strider
2017년 11월 8일
That is the default behaviour of the zeros function. You can always write your own single-argument function to create a column vector of zero elements:
myzeros = @(n) zeros(n,1);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!