users - vector of ones in r




A similar function to R's rep in Matlab (8)

This question already has an answer here:

I am looking for a function that behaves similarly to the rep function in R for Matlab. For example with rep I can do the following:

> rep(c(1,2,3),times=3)
[1] 1 2 3 1 2 3 1 2 3

> rep(c(1,2,3),each=3)
[1] 1 1 1 2 2 2 3 3 3
> 

In matlab there is the repmat function which accomplishes the first part

>> repmat([1,2,3],1,3)

ans =

     1     2     3     1     2     3     1     2     3

but not the second part (or at least I can't figure out how to do it).

Any suggestions?


Good question +1. A neat one-liner method to accomplish this is via the Kronecker tensor product, eg:

A = [1 2 3];
N = 3;
B = kron(A, ones(1, N));

Then:

B =

     1     1     1     2     2     2     3     3     3

UPDATE: @Dan has provided a very neat solution that looks to be more efficient than my kron method, so check that answer out before leaving the page :-)

UPDATE: @bcumming has also provided a nice solution that should scale very nicely when the input vector is large.


How about using kron? It's perfect for this.

kron(A,ones(4, 1))

If like me you have no idea what a Kronecker tensor product is you might be interested in this more intuitive (and actually I think faster) solution:

c(ceil((1:length(c)*n)/n));

so here I used vector indexing to replicate the matrix. For example using the two case you have above we could do:

c = 1:3;
c([1 1 1 2 2 2 3 3 3]) %for each
c([1 2 3 1 2 3 1 2 3]) %for times

so the questions is how do we make a vector [1 2 3 1 2 3 1 2 3] without the very functionality you are requesting. So I made a vector with the number of elements we need i.e. 1:9 and then divide by three and round up (i.e. try ceil((1:9)/3) in the command line.

A bit of benchmarking (I know this stuff should be in loops so maybe this isn't so accurate):

c = 1:3; n = 3;
tic; k = kron(c, ones(1, n)); toc; % 0.000208 seconds.
tic; a = c(ceil((1:length(c)*n)/n)); toc;  % 0.000025 seconds.
clear;
c = 1:1000000; n = 3;
tic; k = kron(c, ones(1, n)); toc; % 0.143747 seconds.
tic; a = c(ceil((1:length(c)*n)/n)); toc;  % 0.090956 seconds.
clear;
c = 1:10000; n = 1000;
tic; k = kron(c, ones(1, n)); toc; % 0.583336 seconds.
tic; a = c(ceil((1:length(c)*n)/n)); toc;  % 0.237878 seconds.

With reshape and repmat

reshape(repmat([1:6],k,1),1,[])

With bsxfun -

reshape(bsxfun(@plus,[1:6],zeros(k,1)),1,[])

On popular demand with floor -

floor(1:1/k:6+(k-1)/k)

well you could try

(A*ones(1,4))' 

a = [1 2 3];
N = 3;

b = reshape(repmat(a,N,1), 1, [])

Fun with matlab

Let n = 6; and k = 2;. Here are some alternatives:

kron(1:n,ones(1,k))

or

ceil(1/k:1/k:n)

or

double(uint64(1:n*k)/k)

Replicate matrix one row at a time

In this particular instance, you could do something along the lines of

A = [1;2;3;4];
B = repmat(A',4,1);
B = B(:);

What this does is replicate A' to create a matrix B:

 1     2     3     4
 1     2     3     4
 1     2     3     4
 1     2     3     4

It then converts it to a single column using B(:).





matlab