matrix dimension must agree error
Ältere Kommentare anzeigen
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))))
Antworten (2)
the cyclist
am 1 Apr. 2012
0 Stimmen
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 Kommentare
parsh
am 1 Apr. 2012
Image Analyst
am 1 Apr. 2012
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
am 1 Apr. 2012
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
am 1 Apr. 2012
Image Analyst
am 1 Apr. 2012
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
am 1 Apr. 2012
0 Stimmen
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 Kommentar
parsh
am 1 Apr. 2012
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!