# Problem computing array, ??? Error using ==> times Matrix dimensions must agree

Hi all,
I am having a problem computing a 1 dimensional matrix, whose elements are made up of sums of one 1-dimensional matrix dot multiplied by one column of a 2-dimensional matrix. Please see the code snippet if this sounds confusing. Why am I getting this error,

??? Error using ==> times
Matrix dimensions must agree

Error in ==> ncorr2 at 11
corr(m)=sum(a.*b(:,m))

Keep in mind that I would call this function like so:
x1 = [1;2;3]
x2 = [4;5;6]
[corr, lags] = ncorr2(x1,x2)

Thanks for all of your help and let me know if I need to be more specific or add any information!
``````function [corr, lags] = ncorr2(x1, x2)
[N,M]=size(x1);
d=1:(2*N-1);
lags(d)=d-N;
a=[zeros(N-1,1);x1;zeros(N+1,1)];
b=zeros((3*N),(2*N+1));
idx=tril(ones((3*N),(2*N+1)))-tril(ones((3*N),(2*N+1)),-N);
b(idx>0)=repmat(x2(:),(2*N+1),1);

m=1:(2*N-1);
corr(m)=sum(a.*b(:,m))
``````
###### Who is Participating?

Commented:
When you do by-element muiltiplication, both matrices should have the same size.
If you want to miltiply vector a to all columns of b, just repeat a to have the same number of columns as in b.
``````corr(m)=sum(repmat(a,1,numel(m)).*b(:,m));
``````
0

Author Commented:
Once again, great job.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.