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))))
回答 (2 件)
the cyclist
2012 年 4 月 1 日
0 投票
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
2012 年 4 月 1 日
Image Analyst
2012 年 4 月 1 日
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?
Walter Roberson
2012 年 4 月 1 日
Different problems. This is "matrix dimension must agree" and the other one is "index exceeds matrix dimensions". The code involved is not the same.
parsh
2012 年 4 月 1 日
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
2012 年 4 月 1 日
0 投票
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.
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!