MATLAB: How to copy each row of a vector according to the number in another vector

basic genetic algorithmrepmatsthocastic uniform selection

Hello, Good night !!
please could you help me … I am trying to execute the procedure in matlab:
I have a vector
A=[1 2;3 4;5 6;7 8;9 10]
two columns and five rows
And I have a vector
quantity_copies=[1;0;2;2;0]
one column and five rows Each element of the vector quantity_copies is the number of times that the correspondent row of vector A will be copied.
I need a vector
B = [1 2;5 6;5 6;7 8;7 8]
I tried this code:
clear all
clc
A=[1 2;3 4;5 6;7 8;9 10]
quantity_copies=[1;0;2;2;0]
n=5
for loop=1:n
B=repmat(A(loop,:),[quantity_copies(loop,1) 1])
end
  • IT WORKS BUT, FOR EACH ITERATION, I LOOSE THE OLD VALUES.
PLEASE WHATS THE SOLUTION FOR THIS ?
I NEED TO MERGE ALL VALUES IN ONLY ONE VECTOR B
THANK YOU … * THIS IS THE RESULT OF MY CODE:
A =
1 2
3 4
5 6
7 8
9 10
quantity_copies =
1
0
2
2
0
n =
5
B =
1 2
B =
Empty matrix: 0-by-2
B =
5 6
5 6
B =
7 8
7 8
B =
Empty matrix: 0-by-2
>>

Best Answer

Hi
the following lines do what you are asking:
A=[1 2;3 4;5 6;7 8;9 10]
qc=[1;0;2;2;0]
qc=qc'
L=find(qc>0)
reps=nonzeros(gc)'
v=0;for k=1:1:numel(L) v=[v repmat(L(k),1,reps(k))]; end
v(1)=[]
A(v',:)
=
1 2
5 6
5 6
7 8
7 8
if you find these lines useful would you please click on the Accept Answer?
thanks in advance for time and attention
John BG