Solved

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

Posted on 2009-02-23
2,736 Views
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))
``````
0
Question by:ehensens

LVL 15

Accepted Solution

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 Closing Comment

Once again, great job.
0

## Featured Post

### Suggested Solutions

sumNumber challenge 16 79
sum13 challenge 24 69
has12 challenge 13 52
countHi2 challenge 7 30
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.