Repeating elements of an array

조회 수: 34 (최근 30일)
daniel adams
daniel adams 2021년 12월 3일
답변: Dave B 2021년 12월 3일
Say for some . How can I create an array of size which is just i.e repeat the elements of x M times each in a row.
Note I have tried using the reshape function but cant work out how to do it in this case.
  댓글 수: 1
James Tursa
James Tursa 2021년 12월 3일
What have you done so far? What specific problems are you having with your code?

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

답변 (1개)

Dave B
Dave B 2021년 12월 3일
There are two really useful repeat functions: repmat and repelem
repelem is good for repeating an element n times, so I think this is what you want. Here I repeat the numbers in a 3 times each:
a=[1 3 7 9];
repelem(a,3)
ans = 1×12
1 1 1 3 3 3 7 7 7 9 9 9
repmat is good when you want to repeat an array of elements, here I repeat a itself 3 times in the row dimension, and 2 times in the column dimension:
repmat(a,3,2)
ans = 3×8
1 3 7 9 1 3 7 9 1 3 7 9 1 3 7 9 1 3 7 9 1 3 7 9

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by