Fill a vector based on another vector

4 次查看(过去 30 天)
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