How to repeat an array based on another array

조회 수: 3 (최근 30일)
Mohammed  Alshehri
Mohammed Alshehri 2017년 1월 15일
답변: Stephen23 2017년 1월 15일
Greetings
I am trying to repeat an array based on another array e.g
Repeat element of array A based on the elements of array B
A= [ 1 2 3 4 ]
B= [ 2 3 1 2]
the resulted array should be ( first element of A that is 1 repeated 2 times which is the first element of array B and so on )
C= [ 1 1 2 2 2 3 4 4 ]

채택된 답변

the cyclist
the cyclist 2017년 1월 15일
Use repelem.
C = repelem(A,B)

추가 답변 (1개)

Stephen23
Stephen23 2017년 1월 15일
For MATLAB before R2015a:
>> A = [1,2,3,4];
>> B = [2,3,1,2];
>> cell2mat(arrayfun(@(a,b)repmat(a,1,b),A,B,'uni',0))
ans =
1 1 2 2 2 3 4 4

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by