I have a vector of the form:
A = [a1 a2 a3 a4 ...];
I want to create another vector like this:
B = [a1 a1 a1 a2 a2 a2 a3 a3 a3 a4 a4 a4 ...];
How can i do this?
I have a vector of the form:
A = [a1 a2 a3 a4 ...];
I want to create another vector like this:
B = [a1 a1 a1 a2 a2 a2 a3 a3 a3 a4 a4 a4 ...];
How can i do this?
There are many ways to do this, for instance using the Kronecker tensor product like this:
B = kron(A,ones(1,3))
or combining reshape
and repmat
like this:
B = reshape(repmat(A,3,1),1,[])
if you're not sure if the vector is horizontal or vertical, you can use (:)
and the transpose .'
like this:
B = kron(A(:).', ones(1,3))
B = reshape(repmat(A(:).',3,1),1,[])
EDIT
You said the reshape version didn't work. Here are the results from my tests:
A = 1:4
B = reshape(repmat(A(:).',3,1),1,[])
B =
1 1 1 2 2 2 3 3 3 4 4 4
A = (1:4)'
B = reshape(repmat(A(:).',3,1),1,[])
B =
1 1 1 2 2 2 3 3 3 4 4 4
So, it works for both column and row vectors (at least for me).
Some more ways to do it:
%# vector-matrix product
reshape(ones(3,1)*A, 1,[])
%# reorganize simple concatenation
nA = 3*numel(A);
B([1:3:nA 2:3:nA 3:3:nA]) = [A A A];