필터 지우기
필터 지우기

repeating elements of a vector in a certain manner

조회 수: 1 (최근 30일)
Mnr
Mnr 2016년 2월 23일
답변: Star Strider 2016년 2월 23일
Hello all, I have a vector A of length n. I would like to create a new vector B which contains each element of A repeated m times; i.e. length of B is mxn. For example, let A=[2 4 5 7 8]; m=3; then, B=[2 2 2 4 4 4 5 5 5 7 7 7 8 8 8]; What is the fastest way of doing that? Thanks!

채택된 답변

Star Strider
Star Strider 2016년 2월 23일
Two options, both producing the same output:
A=[2 4 5 7 8];
B1 = repelem(A,3) % Introduced in R2015a
B2 = reshape(repmat(A, 3, 1), [], 1)'
B1 =
2 2 2 4 4 4 5 5 5 7 7 7 8 8 8
B2 =
2 2 2 4 4 4 5 5 5 7 7 7 8 8 8

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 23일
B = reshape([A;A;A], 1, [])
  댓글 수: 1
Mnr
Mnr 2016년 2월 23일
Thanks! but what about for any m? In other words, how can I write reshape([A;A;A]) for m>3?

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by