필터 지우기
필터 지우기

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