Extend a vector by extending its elements

I have a vector of dimension d: (1,2,...,d) (it's (x_1,x_2,...,x_n) but I removed the 'x_' for simplicity). Now I would like to extend it (in a fast manner) to obtain a d*d vector of the form:
(1,1,...,1,2,...,2,...,d,d,...,d)
where each element repeat d times.
I looked at this page: http://www.mathworks.fr/fr/help/wavelet/ref/wextend.html but it seems to me that the function does not handle this kind of extension.
Thank you for your help.

 採用された回答

Udit Gupta
Udit Gupta 2014 年 5 月 29 日
編集済み: Udit Gupta 2014 年 5 月 29 日

1 投票

If your vector is X.
reshape(repmat(X,d,1),1,[])
should do the trick.
Example -
>> X = [1 2 3 4];
>> d=4;
>> reshape(repmat(X,d,1),1,[])
ans =
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4

3 件のコメント

f10w
f10w 2014 年 5 月 29 日
Thanks, Udit. If X is a column vector, then is there a way to avoid transposing and then re-transposing again the final vector?
Udit Gupta
Udit Gupta 2014 年 5 月 29 日
reshape(repmat(X,d,1),[],1) will give you a column vector. But in this case you will have to transpose the original vector. SO you will apply -
reshape(repmat(X',d,1),[],1)
f10w
f10w 2014 年 5 月 29 日
Thanks, Udit!

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

その他の回答 (3 件)

George Papazafeiropoulos
George Papazafeiropoulos 2014 年 5 月 29 日
編集済み: George Papazafeiropoulos 2014 年 5 月 29 日

0 投票

If X is a column vector:
% initial data
d=10;
% engine
X=(1:d)';
ind=ones(d^2,1);
ind(d+1:d:d^2)=1-d;
% result
output_vector=X(cumsum(ind))
If X is a row vector:
% initial data
d=10;
% engine
X=1:d;
ind=ones(d^2,1);
ind(d+1:d:d^2)=1-d;
% result
output_vector=X(cumsum(ind))
without the need to reshape or repmat!

1 件のコメント

f10w
f10w 2014 年 5 月 29 日
Thanks, George. I will compare with Udit's solution and get back to you.

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

Jos (10584)
Jos (10584) 2014 年 5 月 29 日
編集済み: Jos (10584) 2014 年 5 月 29 日

0 投票

A slower but nice alternative to repmat:
X = [10 20 30 40]
d = 4
Y = kron(X, ones(1,d))
Jos (10584)
Jos (10584) 2014 年 5 月 29 日
編集済み: Jos (10584) 2014 年 5 月 29 日

0 投票

A fast and also nice alternative:
X = [10 ; 20 ; 30 ; 40]
d = 3
Y = X(ceil((1:d*numel(X))/d))

1 件のコメント

f10w
f10w 2014 年 5 月 29 日
Thanks, Jos. I will compare with Udit's solution and get back to you.

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2014 年 5 月 29 日

コメント済み:

2014 年 5 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by