Indices on the left side are not compatible with the size of the right side error

1 次查看(过去 30 天)
So I'm writing code to create a matrix, M that stores various values for theta and x, but i'm unsure as to what to do about the error. Please let me know what I need to correct in order to overcome the error.
M=[];
theta=linspace(0,180,401);
x=linspace(0,10,501);
for i_theta = 1:length(theta)
for i_x = 1:length(x)
M(i_x,i_theta)= 1200*sind(theta(i_theta)) +400*cosd(theta)*x(i_x);
end
end
****************************************************************************
Unable to perform assignment because the indices on the left side are not compatible with the
size of the right side.
Error in Lab7 (line 9)
M(i_x,i_theta)= 1200*sind(theta(i_theta)) +400*cosd(theta)*x(i_x);

采纳的回答

Alex Mcaulley
Alex Mcaulley 2019-3-11
It's a typo in this line
M(i_x,i_theta)= 1200*sind(theta(i_theta)) +400*cosd(theta)*x(i_x);
I think it should be
M(i_x,i_theta)= 1200*sind(theta(i_theta)) +400*cosd(theta(i_theta))*x(i_x);

更多回答(1 个)

Kevin Phung
Kevin Phung 2019-3-11
M=[];
theta=linspace(0,180,401);
x=linspace(0,10,501);
for i_theta = 1:length(theta)
for i_x = 1:length(x)
M(i_x,i_theta)= 1200*sind(theta(i_theta)) +400*cosd(theta(i_theta))*x(i_x);
end
end
you were missing the indexing for cosd(theta)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by