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

3 visualizzazioni (ultimi 30 giorni)
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 Commento
Mario Malic
Mario Malic il 26 Lug 2020
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];

Accedi per commentare.

Risposta accettata

the cyclist
the cyclist il 26 Lug 2020
Modificato: the cyclist il 26 Lug 2020
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 Commenti
the cyclist
the cyclist il 26 Lug 2020
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Lighting, Transparency, and Shading in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by