How can I expand a array with with even zero intervals in between?

조회 수: 1 (최근 30일)
Seba.V
Seba.V 2019년 8월 15일
편집: Bruno Luong 2019년 8월 15일
Suppose I have an array a=[1 2 3], how can i generate a code that would add x-amout of zeros in between each value of the original array?
For example
a=[1 2 3] ----> function or loop (code) -----> a1=[1 0 0 2 0 0 3]

채택된 답변

Bruno Luong
Bruno Luong 2019년 8월 15일
편집: Bruno Luong 2019년 8월 15일
nz = 2;
b = a;
b(end+nz,:) = 0;
b = b(1:end-nz)
  댓글 수: 2
Seba.V
Seba.V 2019년 8월 15일
Thank you !
Could you please tell me how the last two line of code work though please?
Sorry i am just starting out!
Bruno Luong
Bruno Luong 2019년 8월 15일
편집: Bruno Luong 2019년 8월 15일
To understand you must aware that MATLAB store array column-wise, meaning
A = [1 3 5;
2 4 6];
is stored in contiguous internally 1, 2, 3, 4, 5 6, and when you RESHAPE A by
A(1:4), you'll get a row vector [1 2 3 4].
Now with that in mind, this first statement add nnz rows of zeros in B, so after reshaping you'll get NNZ zeros in between elements of the first row.
The last statement also removes the last NNZ zeros after the last element of a.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by