필터 지우기
필터 지우기

Running code and getting error about vector must be the same length

조회 수: 2 (최근 30일)
I'm running my code and I am getting an error about how the vector must be the same length. Does anyone have advice because I thought the vectors were fillled to the same length?
ntrails = 20;
N_run = zeros(ntrails,1);
BC = [5,-5,-5,5];
for trail = 1:1:ntrails
xA = -5; xAK = xA;
yA= 0; yAK = yA;
xB = 5; xBK = xB;
yB = 0; yBK = yB;
nsteps = 1000;
collision_flag = 0; k = 0;
while collision_flag == 0 && k < nsteps
[xAKp1, yAKP1] = RandWalk_2D(xAK,yAK,BC);
[xBKp1, yBKP1] = RandWalk_2D(xBK,yBK,BC);
xAK_val = [xAK-.5, xAK + .5, xAK + .5, xAK -.5];
yAK_val = [yAK-.5, yAK + .5, yAK + .5, yAK - .5];
xAKp1_val = [xAKp1-.5, xAKp1 + .5, xAKp1 + .5, xAKp1 -.5];
yAKp1_val = [yAKp1-.5, yAKp1 + .5, yAKp1 + .5, yAKp1 - .5];
xBK_val = [xBK-.5, xBK + .5, xBK + .5, xBK -.5];
yBK_val = [yBK-.5, yBK + .5, yBK + .5, yBK - .5];
xBKp1_val = [xBKp1-.5, xBKp1 + .5, xBKp1 + .5, xBKp1 -.5];
yBKp1_val = [yBKp1-.5, yBKp1 + .5, yBKp1 + .5, yBKp1 - .5];
figure(1)
hold on
xlim([-5.5,5.5,])
ylim([-5.5,5.5,])
fill(xAK_val,yAK_val,'r') %current step A
fill(xAKp1_val, yAKp1_val, 'b')%next step for A
fill(xBK_val,yBK_val,'r') %current step B
fill(xBKp1_val, yBKp1_val, 'b')%next step for B
hold off
xAK = xAKp1; yAK = yAKp1;
xBK = xBKP1; yBK = yBKp1;
k = k+1;
if xAK == xBK && yAK == yBK
collision_flag = 1;
N_run(trail) = k;
end
end
end
medval = median(N_run)
function [x,y] = RandWalk_2D(x0,y0,BC)
r = rand;
if r<0.2
x = x0;
y = y0+1;
if y >= BC(1); y = BC(1);
end
else if 0.2 < r && r <= 0.4;
x = x0;
y = y0-1;
if y <= BC(2);
y = BC(2);
end
elseif 0.4< r && r<=0.6
x = x0 - 1;
y = y0;
if x<= BC(3); x = BC(3)
end
elseif 0.6 < r && r <= 0.8;
x = x0+1; y = y0;
if x >= BC(4); x = BC(4);
end
elseif 0.8<r
x = x0; y = y0;
end
end
end
  댓글 수: 1
Mario Malic
Mario Malic 2020년 7월 26일
The code is not working. Meanwhile, you can call the variables in the command window that report the error and check what's the issue. Also, post the error in the future.
Undefined function or variable 'yAKp1'.
Error in Marios (line 19)
yAKp1_val = [yAKp1-.5, yAKp1 + .5, yAKp1 + .5, yAKp1 - .5];

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

채택된 답변

the cyclist
the cyclist 2020년 7월 26일
편집: the cyclist 2020년 7월 26일
It's the subtle difference between these two lines of code:
xAK_val = [xAK-.5, xAK + .5, xAK + .5, xAK -.5];
yAK_val = [yAK-.5, yAK + .5, yAK + .5, yAK - .5];
Specifically, the last element of each of these vectors.
yAK - .5
is a single element, equal to yAK minus 0.5. But
xAK -.5
is two elements, xAK and -0.5.
(Note that you have that same difference in several places.)
  댓글 수: 3
the cyclist
the cyclist 2020년 7월 26일
In case you are unaware, the MATLAB debugger is a very useful tool for tracking down things like this.
Specifically, I stopped your code when the error occurred, by using
dbstop if error
then traced back why those two lines of code were different lengths.
Marios Christofides
Marios Christofides 2020년 7월 26일
Thank you!!! I would have not caught that.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by