user3482383
user3482383

Reputation: 295

Alternative of repmat for large vectors in matlab

I am writing a code in which the speed and time reduction is really important. I have to create some matrices by repeating some vectors. As my vectors are almost large, using repmat to create these matrices takes relatively a long time. Is there any other way to reduce this time?

Upvotes: 1

Views: 3192

Answers (1)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38042

Our test vector:

V0 = [1; 2; 3]; 

Tony's trick:

V1 = V0(:, ones(3,1))

Matrix/vector multiplication:

V2 = V0 * ones(1,3);    

Kronecker product:

V3 = kron(V0, ones(1,3))

Concatenation:

V4 = [V0 V0 V0]

Inobvious way:

V5 = arrayfun(@(~)V0, 1:3, 'uniformoutput', false);
V5 = cat(2, V5{:});

The least obvious way:

[Vi{1:3}] = deal(V0);
V6 = [Vi{:}]

Use repmat from the Lightspeed toolbox.

Or, the best way, use implicit expansion (R2008 and up):

%# NOTE: this PREVENTS having to do explicit replication, and carries 
%# out the multiplication re-using the same elements from V0: 
M = bsxfun(@times, rand(1,5), V0)

Upvotes: 4

Related Questions