Inner matrix dimensions must agree.

1 view (last 30 days)
Jack
Jack on 28 Oct 2014
Answered: dpb on 28 Oct 2014
Hi, having a little trouble making it multiply.
Sorry but I am fairly new to the software, and so have probably missed something very basic.
function move(n)
%column 1 is x, 2 is y, 3 is velocity, 4 is angle (radians)
P=10*rand(n,4);
plot(P(1:10,1),P(1:10,2),'o')
for t=0:0.01:20
P=P+(P(1:n,3)*(P(1:n,1)*cos(P(1:n,4))+P(1:n,2)*sin(P(1:n,4))));
plot(P(1:10,1),P(1:10,2),'o')
pause(0.5);
end
Any help would be great.

Answers (2)

Ganesh Gaonkar
Ganesh Gaonkar on 28 Oct 2014
From your question, It was not clear to me, what value of 'n' you are passing to this function. However, you can just put a break point in the line above the one where the error occurs to see if all arguments/variables are getting calculated with right dimensions.That way you can check the value of each variables in debug mode and locate the cause of the issue.

dpb
dpb on 28 Oct 2014
P=P+(P(1:n,3)*(P(1:n,1)*...
'*' is matrix multiply in Matlab and P(1:n,N) is a column vector of size nx1. To multiply two it would have to be nx1 X 1xn or 1xn X nx1 depending on whether you intend the result to be n-by-n or 1-by-1.
Or, if you intend the result to be the element-wise product of the two vectors and the result to be nx1 then
doc times
for the "dot" operator.

Community Treasure Hunt

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

Start Hunting!