I'm getting a matrix error of inconsistently on line 45, any tips how I can fix it.

2 visualizaciones (últimos 30 días)
N=10;
L=10;
D=.01;
dt=.01;
k_on=1;
k_off=.1;
F=1;
tmax=100;
x=L*rand(N,1);
y=L*rand(N,1);
state=zeros(N,1);
figure;
axis([0 L 0 L]);
set(gca,'nextplot','replacechildren');
for t = 0:dt:tmax
dx=sqrt(2*D*dt)*randn(N,1);
dy=sqrt(2*D*dt)*randn(N,1);
x=x+dx;
y=y+dy;
x(x<0)=0;
x(x>L)=L;
y(y<0)=0;
y(y>L)=L;
p_on=k_on*dt*F;
p_off=k_off*dt;
for i = 1:N
if state(i)==0
if rand < p_on
state(i)=1;
end
else
if rand<p_off
state(i)=0
end
end
end
colors= repmat([1 0 0], N,1);
colors(state == 1,:,:) = repmat([0 1 0],sum(state),1);
scatter(x,y,50,colors,'filled');
title(sprintf('Time=%.2f',t));
drawnow;
if any(state == 1)
p_bind=1-exp(-k_on*dt*F);
if rand<p_bind
[~,i]=max(state);
state(i)=2;
colors(i,:)=[0]
end
end
end
colors = 10×3
1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0
Unable to perform assignment because the size of the left side is 2-by-3 and the size of the right side is 4-by-3.

Respuestas (1)

LeoAiE
LeoAiE el 1 de Mayo de 2023
It seems there is an issue with the assignment of the colors variable in the last part of your code.
N=10;
L=10;
D=.01;
dt=.01;
k_on=1;
k_off=.1;
F=1;
tmax=100;
x=L*rand(N,1);
y=L*rand(N,1);
state=zeros(N,1);
figure;
axis([0 L 0 L]);
set(gca,'nextplot','replacechildren');
for t = 0:dt:tmax
dx=sqrt(2*D*dt)*randn(N,1);
dy=sqrt(2*D*dt)*randn(N,1);
x=x+dx;
y=y+dy;
x(x<0)=0;
x(x>L)=L;
y(y<0)=0;
y(y>L)=L;
p_on=k_on*dt*F;
p_off=k_off*dt;
for i = 1:N
if state(i)==0
if rand < p_on
state(i)=1;
end
else
if rand<p_off
state(i)=0
end
end
end
colors = repmat([1 0 0], N,1);
colors(state == 1,:,:) = repmat([0 1 0],sum(state == 1),1);
scatter(x,y,50,colors,'filled');
title(sprintf('Time=%.2f',t));
drawnow;
if any(state == 1)
p_bind=1-exp(-k_on*dt*F);
if rand<p_bind
[~,i]=max(state);
state(i)=2;
colors(i,:)=[0 0 1]; % Corrected color assignment
end
end
end
  2 comentarios
BAILEY MCMASTER
BAILEY MCMASTER el 1 de Mayo de 2023
I tried this and I'm still getting the matrix error after running it.
BAILEY MCMASTER
BAILEY MCMASTER el 1 de Mayo de 2023
Actually nevermind, one of my ends was in the wrong spot, it works. Thank you so much!

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by