Fill a vector based on another vector

4 Ansichten (letzte 30 Tage)
Pilar Jiménez
Pilar Jiménez am 21 Okt. 2019
Bearbeitet: Pilar Jiménez am 21 Okt. 2019
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.

Antworten (1)

Yuan Li
Yuan Li am 21 Okt. 2019
A(1:sum(B(:))) = 0;
I hope the code can help you.
  1 Kommentar
Pilar Jiménez
Pilar Jiménez am 21 Okt. 2019
Bearbeitet: Pilar Jiménez am 21 Okt. 2019
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by