Clear Filters
Clear Filters

error in a loop

2 views (last 30 days)
Kugen Raj
Kugen Raj on 20 Mar 2012
for n =1:N
for radius = 1:R
% Random x & y numbers.
theta = rand(1,n)*(2*pi);
r = sqrt(rand(1,n))*radius;
end
x(n)= x1 + r.*cos(theta);
y(n)= y1 + r.*sin(theta);
end
plot(x,y,'.')
i am creating a random plot inside a circle using for command. i am having an error:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> Q11 at 53
x(n)= x1 + r.*cos(theta);
how can i overcome this?
  1 Comment
Wayne King
Wayne King on 20 Mar 2012
you do not show us what x1 and y1 are.

Sign in to comment.

Accepted Answer

Daniel Shub
Daniel Shub on 20 Mar 2012
Just like the error says the number of elements in B and in I must be the same. For you, B is
x1+r.*cos(theta)
with the number of elements being equal to the number of elements in r and I is
n
with 1 element.
Are you sure you want
theta = rand(1,n)*(2*pi);
r = sqrt(rand(1,n))*radius;
and not
theta = rand*(2*pi);
r = sqrt(rand)*radius;

More Answers (0)

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!