Combining two arrays by pointwise repeating the elements of A(i), B(i) times.

2 Ansichten (letzte 30 Tage)
Michel
Michel am 9 Okt. 2014
Bearbeitet: Matt J am 9 Okt. 2014
I want to combine two vectors (A and B) with the same length into a new vector (C) with length sum of B.
e.g.
A = [1,2,3,4]; B = [2,1,2,3];
C = [1,1,2,3,3,4,4,4]
Thus repeating the elements of A, B times.

Antworten (2)

Pierre Benoit
Pierre Benoit am 9 Okt. 2014

Matt J
Matt J am 9 Okt. 2014
Bearbeitet: Matt J am 9 Okt. 2014
idx(cumsum([1,B]))=ones(length(A)+1,1);
C=A(cumsum(idx(1:end-1)))

Kategorien

Mehr zu Matrices and Arrays 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