Fill a vector based on another vector

조회 수: 4 (최근 30일)
Pilar Jiménez
Pilar Jiménez 2019년 10월 21일
편집: Pilar Jiménez 2019년 10월 21일
I know the title is somewhat confusing, but I hope you can help me. I need to make a code for a specific application, I am confused on how to do a part. I have the following:
Nmax=25;
A=ones(1:Nmax);
B=[1;1;6;12,1,2,5,2];
What I need to do is fill vector A with the number of zeros indicated by vector B, that is, vector A changes from ones to zeros based on vector B in the order in which it is found. For example:
The first and second values of B indicate that it is 1 zero, therefore, vector A = [00111 ...]
The third value of B indicates that there are 6 zeros, therefore, vector A = [0000000011111 ...]
And so on.
If the number of zeros exceeds the value of Nmax, save it in another vector and cut the count to the integers of B.
The logic of the code should be like this, I was already trying index by index with a for cycle, but it makes errors.
Thank you in advance for your help.

답변 (1개)

Yuan Li
Yuan Li 2019년 10월 21일
A(1:sum(B(:))) = 0;
I hope the code can help you.
  댓글 수: 1
Pilar Jiménez
Pilar Jiménez 2019년 10월 21일
편집: Pilar Jiménez 2019년 10월 21일
Thanks for answering.
Sorry, I edited the question. It happens that the sum of B exceeds the size of A, so I must work in this case index by index.
When exceeding I must keep the last 5 and 2 zeros in another vector, so I will also add a conditional.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by