Extend a vector by extending its elements
Show older comments
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.
Accepted Answer
More Answers (3)
George Papazafeiropoulos
on 29 May 2014
Edited: George Papazafeiropoulos
on 29 May 2014
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!
Jos (10584)
on 29 May 2014
Edited: Jos (10584)
on 29 May 2014
A slower but nice alternative to repmat:
X = [10 20 30 40]
d = 4
Y = kron(X, ones(1,d))
Jos (10584)
on 29 May 2014
Edited: Jos (10584)
on 29 May 2014
A fast and also nice alternative:
X = [10 ; 20 ; 30 ; 40]
d = 3
Y = X(ceil((1:d*numel(X))/d))
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!