In an assignment A(I) = B, the number of elements in B and I must be the same.

clc
x=25;
y=25;
for i=1:10
r=rand;
if r<=0.25
x(i) =x+1
elseif r<=0.5 && r>0.25
y=y+1
else if r<=0.75 && r>0.5
x=x-1
else
y=y-1
end
end
x=x;
y=y;
end
comet(x,y)
plot(x,y)
end
The above code generates the error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in xy (line 7)
x(i) =x+1
Can you please help me in modifying the code in right way so that I can balance the vectors.
I am really sorry for asking this kind of question again and again as I have seen the repetitions, but it was a failure to gain the correct answer. Thank You.

댓글 수: 2

Learn to use DBSTOP and breakpoints.
I am not using DBSTOP since this is the point where i am having the error. I need to use the array and am unclear on implementing it as a vector.

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2012년 10월 11일
편집: Image Analyst 2012년 10월 11일
Try
x(i) = x(i) + 1;
Full Demo:
clc;
clearvars;
close all;
workspace;
format longg;
format compact;
fontSize = 16;
numberOfSteps = 10;
r = rand(numberOfSteps, 1);
x = zeros(numberOfSteps, 1);
y = zeros(numberOfSteps, 1);
x(1) = 25; % Starting location.
y(1) = 25; % Starting location.
subplot(2, 2, 3:4);
hold on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
for k = 2 : numberOfSteps
if r(k) <= 0.25
x(k) = x(k-1) + 1 % Move x by 1.
y(k) = y(k-1); % No change;
elseif r(k) <= 0.5 && r(k) > 0.25
x(k) = x(k-1); % No change;
y(k) = y(k-1) + 1 % Move y by 1.
elseif r(k) <= 0.75 && r(k) > 0.5
x(k) = x(k-1) - 1 % Move x by 1.
y(k) = y(k-1); % No change;
else
x(k) = x(k-1); % No change;
y(k) = y(k-1) - 1 % Move y by 1.
end
text(x(k)+0.05, y(k), num2str(k), 'fontSize', fontSize);
end
subplot(2, 2, 1:2);
comet(x,y);
grid on;
title('Comet', 'fontSize', fontSize);
subplot(2, 2, 3:4);
plot(x, y, 'bs-', 'LineWidth', 2);
title('Plot', 'fontSize', fontSize);
grid on;

댓글 수: 1

Thank You very much Image Analyst. I got the answer from you, thank you. Solution for those with similar problem:
x = zeros(numberOfSteps, 1); <== This step is essential as it creates the Zero array.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Fig
Matt Fig 2012년 10월 11일

0 개 추천

You are trying to put a vector into a single element of x.... That won't work. What are you trying to do here? It looks like you are trying to build an array x, but what about y? Do you want to build that as an array too?

댓글 수: 2

Thanks Matt, I need to create a 2D array wherein I need to move x & y independently depending on the random number case. So I need to build an array and plot the graph.
And yes, I would like to build an array with y too, but if the x(i) function works, i will be able to proceed with y the same way.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2012년 10월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by