matrix dimension must agree error

3 次查看(过去 30 天)
parsh
parsh 2012-4-1
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))))

回答(2 个)

the cyclist
the cyclist 2012-4-1
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 个评论
parsh
parsh 2012-4-1
this is different question. the one which was solved was another question
Image Analyst
Image Analyst 2012-4-1
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?

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2012-4-1
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 个评论
parsh
parsh 2012-4-1
yes, i changed the theta and it works fine, i guess it was mistake in question maybe.
thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by