Clear Filters
Clear Filters

I got this Error :Matrix dimensions must agree

1 view (last 30 days)
Q1: When i input this formula ; f=(4/pi)*sum((1./n)*sin(2*pi*n.*t)) I got this error: Matrix dimensions must agree
Where n=1:2:401 and t=linspace(-0.5,0.5,250)
Q2: f=1/pi+0.5*sin(pi*tau)-2/pi*(cos(pi*n'*tau)/n'.^2-1) tau=linspace(-0.5,0.5,250); n=2:2:106; i get the same error !
Please correct it for me. And show me where are the mistakes

Answers (3)

Andrei Bobrov
Andrei Bobrov on 18 Sep 2012
Edited: Andrei Bobrov on 18 Sep 2012
one way
%W1:
n = (1:2:401).';
t = linspace(-0.5,0.5,250);
f = 4/pi*sum(bsxfun(@times,1./n,sin(2*pi*n*t)));
%W2:
n = (2:2:106).';
tau = linspace(-0.5,0.5,250);
f = 1/pi+bsxfun(@minus,0.5*sin(pi*tau),2/pi*(bsxfun(@rdivide,cos(pi*n*tau),n.^2)-1));
  2 Comments
Jan
Jan on 18 Sep 2012
Mohammed has written [MOVED from answer to comment section]:
But i need 250 variables for t

Sign in to comment.


Jan
Jan on 18 Sep 2012
for the expression n .* t the variables n and t must have the same size. Perhaps you want a matrix like, e.g. created by a dyadic product:
n(:) * t
But as long as you do not specify this, n .* t is simply a bug, when the sizes differ or non of them is a scalar.

Mohammed
Mohammed on 18 Sep 2012
Non of answers work !
  1 Comment
Jan
Jan on 18 Sep 2012
Edited: Jan on 18 Sep 2012
Please, Mohammed, do not post comments to answers as answer, but as comment. When you claim, that a suggested method "does not work", be so kind to show the code, the error message or an explanation of the difference between your expectation and the results.
Of course my suggestion will "not work", because I told you, that there is a bug in your code or that you at least did not tell us, what you want to achieve exactly enough. How could this be "working"?!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!