I want to repeat an array by another array

조회 수: 3(최근 30일)
Joshua Folorunso
Joshua Folorunso 2022년 11월 12일
댓글: Joshua Folorunso 2022년 11월 12일
Hi, all.
I have this problem that look rather simple. I have a vector say
A= [ 1,2,3,4,5]
and another vector
B= [2, 3,1,5,7,2]
I want to repeat A by the number of times in B to get a vector
C= [1,1, 2,2,2,3,4,4,4,4,4,4,4,5,5]
i.e. I repeated the first element in A by the number of first element in B and so on. I think there should be a simple syntax for it. Please help. Thank you.

채택된 답변

the cyclist
the cyclist 2022년 11월 12일
편집: the cyclist 2022년 11월 12일
A = [1,2,3,4,5];
B = [2,3,1,7,2]; % You had a mismatch in the length of B compared to A
C = repelem(A,B)
C = 1×15
1 1 2 2 2 3 4 4 4 4 4 4 4 5 5
  댓글 수: 1
Joshua Folorunso
Joshua Folorunso 2022년 11월 12일
You are right,
Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by