[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2802
  • Last Modified:

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))

Open in new window

0
ehensens
Asked:
ehensens
1 Solution
 
yuk99Commented:
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));

Open in new window

0
 
ehensensAuthor Commented:
Once again, great job.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now