3 views (last 30 days)

I habe two vectors and with . I am implemented the following code

z = rand(length(x),1);% Just some fake data to define the size of z. could also be z=zeros(size(x))

for i=m+1:n+1

z(i)=x(i-m:i-1)'*y;

end

Knowing that n get have over a few million elements and m is less than 200, the computations rapidely become slow. How can I vectorize this operation to optimize it?

Thanks for your help!

Bruno Luong
on 8 Oct 2019

n=10

m=3

x=rand(n,1)

y=rand(m,1)

% your method

z = nan(length(x),1);% Just some fake data to define the size of z. could also be z=zeros(size(x))

for i=m+1:n+1

z(i)=x(i-m:i-1)'*y;

end

z

% My method

z = [nan(m,1); conv(x,flip(y),'valid')]

Rik
on 8 Oct 2019

Bruno Luong
on 8 Oct 2019

Well that comes straighforward from the definition of CONV, it performs a slighding sum with one of the array that is straight and another is flipped.

So if one doesn't want to flip dusing the sum, one have to flip the array before calling CONV.

Sign in to comment.

Daniel M
on 7 Oct 2019

Edited: Daniel M
on 7 Oct 2019

If you have the signal toolbox, you can use the buffer() command to an array of the x values that you require, then do the matrix multiplication in one shot.

x = 1:50;

m = 6;

L = 15;

n = 48;

z = buffer(x(L+1-m:n),m+1,m,'nodelay'); % size(z) = [7,33]

y = 1:7; % size(y) = [1,7]

vals = y*z; % size = [1,33]

% if no signal toolbox, try

% ind = (L+1:n)-m + (0:m)';

% z = x(ind);

I'm not sure if this will be faster. It could require a lot of memory. But it is vectorized, so test it out and see.

Daniel M
on 7 Oct 2019

So do it in chunks. I don't know your computer specifications. It works on my computer fine, and takes 34 seconds to make an array that large. You say the code is slow, but not how slow. You say you want it faster but don't specify what will satisfy your goals.

Are you just impatient? Or is there a strict requirement to compute in a certain time?

I suggest either waiting for the computations to finish, buy a better computer, or reevaluate your code and maybe you don't need to do this calculation in the first place.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753637

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753637

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753641

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753641

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753642

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753642

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753653

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753653

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753655

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753655

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753670

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/484058-how-vectorize-this-operation#comment_753670

Sign in to comment.