matrix dimension must agree error

here i did some coding again but i get this error
"Error using .*
Matrix dimensions must agree."
theta=(0:pi/20:pi/2)'
d= (1:1:10)'
meshgrid(theta,d)
Dc(d,theta)= (d/2).*(1-cos(theta)); <---- error in this line
Q= (2.^(3/2).*Dc.^(5/2).*sqrt(g).*(theta-0.5.*sin(2.*theta)).^(3/2))/(8.*sqrt (sin(theta).*(1-cos(theta).^(5/2))))

Answers (2)

the cyclist
the cyclist on 1 Apr 2012
There are two errors in that line.
First, on the right-hand side, d and theta and length 10 and 11, respectively, so you cannot do a vector operation on them. You could start theta at pi/20 instead of 0 to solve that.
Second, on the left-hand side, you are trying to index into an array using a non-integer. That won't work.

5 Comments

its a set value so i have to use theta and d as they are, and i tried
Dc(d,theta)= (d/2)*(1-cos(theta));
i still get the same error
But you already marked it solved and working in your duplicate request: http://www.mathworks.com/matlabcentral/answers/34141-why-do-i-keep-getting-index-exceeds-matirx-dimensions Is it working or not? Why do you have two postings on this?
Different problems. This is "matrix dimension must agree" and the other one is "index exceeds matrix dimensions". The code involved is not the same.
this is different question. the one which was solved was another question
All right. But may I suggest that learning how to use the debugger would quickly reveal the problems for these "matrixes/indexes don't match" kinds of errors?

Sign in to comment.

Walter Roberson
Walter Roberson on 1 Apr 2012
cos(theta) is going to be the same size as theta.
1 - cos(theta) is going to be the same size as cos(theta), and thus the same size as theta.
(d/2) is going to be the same size as d.
(d/2) .* (1 - cos(theta)) will fail if (d/2) is not the same size as (1-cos(theta)), and thus will fail if d is not the same size as theta. The Cyclist pointed out that d and theta are in fact different sizes, and you indicate that you are not permitted to change the values. You will not be able to get that line of code to work on those variables. This should suggest to you that the line of code is incorrect.
If d and theta were the same length, then (d/2) .* (1 - cos(theta)) would be the same size as d (same as theta). But look at what you are trying to assign the values to: a matrix of size length(d) by length(theta). This should suggest to you that something is seriously wrong with that line of code.
Note (HINT!): your existing code would be more efficient if you were to remove the meshgrid() call, since you do not assign the output of meshgrid to anything. It is a waste of time to construct that length(d) by length(theta) matrix and not do anything with it.

1 Comment

yes, i changed the theta and it works fine, i guess it was mistake in question maybe.
thanks

Sign in to comment.

Categories

Tags

Asked:

on 1 Apr 2012

Community Treasure Hunt

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

Start Hunting!