# MATlab: How to expand an array

Status
Not open for further replies.

#### RollingEEE

##### Full Member level 3
matlab repeat vector

Hi,

I am looking for a code that will repeat each column of a vector to produce a new vector. My code is extremely time consuming. Even matlab suggests not to use an array that gradually 'blows up'

Code:
function y = expand(b, m)
A = [];
n = length(b);
for i = 1:n
A = [A ones(1, m) .* b(:, i)];
end
y = A;
Can any one help?

#### sau_sol

##### Full Member level 5
matlab array

its working for small values

what input you are giving please specify

#### RollingEEE

##### Full Member level 3
expand matrix matlab

It works for all values. But if the matrix is large (say 4x2000), it takes a hell lot of time

#### sau_sol

##### Full Member level 5
matlab repeat array

yeah it will take because it has to do a lot of calculation and then to store it in a variable in RAM or HDD whatever but you require a really fast computer to work with such big calculations

#### RollingEEE

##### Full Member level 3
matlab expand

Any one else with some actual skills of matlab?

#### Communications_Engineer

matlab repeat

RollingEEE said:
Any one else with some actual skills of matlab?
For a matrix size of 4x2000, it will always take a lot of time.

Matlab often gives this warning that the variable might be growing, but usually it does not effect the performance too much.

Anyway, if you dont want to use this method, then ask the user to input the size of the matrix vectors (b,m) as the input to the function, then create a variable like
A = zeros(1,L), where L can be the length of L

This way the variable A will not be growing inside the loop

Of course, you'll then need to make some modifications to the code, where you assign values to variable A, like this A(i) = ....

where i represents the current index of the loop from 1:n

Try this procedure, then compare via the Matlab Profiler to see which process took more processing time.

#### AndyECE

##### Member level 5
matlab expand matrix

I am looking for a code that will repeat each column of a vector to produce a new vector...

-------------------------
So if I understand you correctly, you want a vector
a = [1 2; 3 4];

and you pass expand (a, 3) you get
1 1 1 2 2 2
3 3 3 4 4 4
if this is correct, the proper way of implementing it it is like this:

function y = expandB(b, m)
y=reshape(repmat(b',1,m)', length(b,1)), m*length(b(1,: )));

That's it. As a test, I passed
b = ones(100, 1000)
m = 500

my version took 1.318641 seconds. Thats tiling out a 100x1000 matrix 500 times!

Communications_Engineer said "For a matrix size of 4x2000, it will always take a lot of time"

This is wrong, wrong, wrong. Matlab LOVES to process matrices, you got gotta understand how it does it so you don't artificially slow it down.

The general rule for Matlab is "If you are using a for loop, you are probably doing it wrong."

Seriously, Matlab is NOT optimized for looping. The more you learn to vectorize everything, the better your programs will work

### RollingEEE

points: 2

#### RollingEEE

##### Full Member level 3
matlab expand array

Thanks. But I bet my answer is the best.

use kron (A, ones(1, n))

to expand matrix. One line and simple

Communications_Engineer said:
RollingEEE said:
Any one else with some actual skills of matlab?
For a matrix size of 4x2000, it will always take a lot of time.
Huh, my code used to take 15 mins, now it takes 2 seconds to run with a 4 x 80000 matrix

#### AndyECE

##### Member level 5
expand vector matlab

Nicely done with the use of the 'kron' function. You are clearly making a lot of improvement in your Matlab skills, RollingEEE.

However, my solution is still faster. This function will compare the two:

function y = expandB(b, m)
tic
y=reshape(repmat(b',1,m)', length(b,1)), m*length(b(1,: )));
disp('using reshape/repmat')
toc
tic
y = kron(b, ones(1,m));
disp('using kron')
toc

reshape/repmat takes just about 1/2 the time to run. Granted, we are talking about the difference between 1 second and 2 seconds, but this is a forum, so it is worth comparing different solutions to the same problem.

Mostly, this whole situation really illustrates the need to understand how Matlab operates, and the difference it makes when an intuitive solution (the use of a for loop) is used instead of learning about the programming environment.

hmmm, interesting. It turns of that for an m x n matrix, if n >> m, mine works faster, but if m >> n, then yours works faster.

I'll have to ponder this for a while, and see if I can come up with a reasonable explanation

### RollingEEE

points: 2

#### RollingEEE

##### Full Member level 3
matlab expand vector

You are probably right, but I don't think the reshape method works when you are expanding matrix, like

can it make
[1 2 3; 4 5 6]

as [1 1 1 2 2 2 3 3 3; 4 4 4 5 5 5 6 6 6] ?

Kron can do it

#### AndyECE

##### Member level 5
for loop matlab expand

Yes, the two functions are equivalent. Try it out for yourself!

>> a

a =

1 2 3
4 5 6

>> expandB(a, 3)
using reshape/repmat
Elapsed time is 0.000406 seconds.

ans =

1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6

RollingEEE

points: 2