How can I repeat each element of a vector different times and store them in a new vector

2 ビュー (過去 30 日間)
Dear all,
I am a Matlab novice. I am sorry that my question may be silly.
Does anyone know that how I can repeat each element of a vector different times and then store them in a new vector?
Thank you in advance for the answers
Cheers!
  1 件のコメント
mahmoud osama
mahmoud osama 2020 年 3 月 28 日
vector=[1 2 3]
vector = repele(vector,3)
this will repeat every element 3 times
output:
vector=[1 1 1 2 2 2 3 3 3]

サインインしてコメントする。

採用された回答

Wayne King
Wayne King 2013 年 2 月 19 日
  2 件のコメント
Yufan Miao
Yufan Miao 2013 年 2 月 20 日
Thank you very much for this quick reply. It really helps!

サインインしてコメントする。

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2013 年 2 月 20 日
編集済み: Andrei Bobrov 2013 年 2 月 21 日
r = [1 3 3 4 0 5];
x = 1:6;
t = r > 0;
a = cumsum(r(t));
b = zeros(1,a(end));
b(a - r(t) + 1) = 1;
x1 = x(t);
out = x1(cumsum(b));

Jos (10584)
Jos (10584) 2013 年 2 月 19 日
Wayne pointed you to a run-length decoder/encoder.
Things become more simple if every element is to be repeated the same number of times:
% repeat all elements of A N times
A = [1 2 3]
N = 3 ;
B1 = reshape(repmat(A(:).',N,1),1,[])
% or
nA = numel(A) ;
B2 = A(ceil((1:nA*N)/nA))

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by