필터 지우기
필터 지우기

How to Create array with repeating values of another array

조회 수: 5 (최근 30일)
David Quilligan
David Quilligan 2016년 1월 28일
댓글: David Quilligan 2016년 1월 28일
Hi I have an array say x = [1 2 3 4 5] generated from previous code. I want to then create an array y = [1 1 2 2 3 3 4 4 5 5] what is the easiest way to accomplish this?

답변 (2개)

Brendan Hamm
Brendan Hamm 2016년 1월 28일
x = [1 2 3 4 5];
y = repelem(x,2)
y =
1 1 2 2 3 3 4 4 5 5

Titus Edelhofer
Titus Edelhofer 2016년 1월 28일
Hi David,
use repmat and linear indexing:
x = [1 2 3 4 5];
% repeat two times:
xx = repmat(x, 2, 1);
% and access all elements
xRep = xx(:)'
xRep =
1 1 2 2 3 3 4 4 5 5
Titus

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by