Hello, I have this vector that is constantly changing values for instance the below output might look something like
v =
4 15
3 15
3 16
3 15
2 15
And this will continue to happen till the step size is reached. I do not want any value in the array to repeat ! How do I do this? So 3 15 was repeated twice, I want to force the program to move to a different coordinate versus repeating values. See code below
if true
%Random walk in 2D, periodic boundaries
%The script simulates a random walk with periodic boundary conditions. The
xmax = 20; %grid size
ymax = 20; %grid size
nsteps = 400; %number of steps in the simulation
x = round(5);
y = round(15);
plot(x,y,'bo')
ht = title('Steps taken = 0');
h = [];
axis([0.8 xmax+0.2 0.8 ymax+0.2])
hold on
v=[]
for i = 1:nsteps
d = randi(4); %direction (north(1), west(2), south(3), east(4))
dx = 0;
dy = 0;
switch d
case 1
dy = 1;
case 2
dx = -1;
case 3
dy = -1;
case 4
dx = 1;
end
nx = x + dx;
ny = y + dy;
v(end+1,:)=[nx,ny] %Recording all the steps take
%Now apply periodic boundary conditions
if nx<1
x = x + xmax;
nx = nx + xmax;
elseif nx>xmax;
x = x - xmax;
nx = nx - xmax;
end
if ny<1
y = y + ymax;
ny = ny + ymax;
elseif ny>ymax;
y = y - ymax;
ny = ny - ymax;
end
%Draw a line from the current position to the next position
if ~isempty(h)
set(h,'MarkerEdgeColor','b')
end
line([x nx],[y ny])
h = plot(nx,ny,'r*');
set(ht,'String',['Steps taken = ' num2str(i)])
x = nx;
y = ny;
pause(0.1) %the pause that refreshes
end
hold off
end

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 19일
편집: Azzi Abdelmalek 2014년 11월 19일

0 개 추천

unique(v,'rows')
%Or maybe you want something like this:
v=[4 15; 3 15]
c=[3 15]
if ismember(c,v,'rows')
%skip
else
%do
end

댓글 수: 3

Jessica Benidict
Jessica Benidict 2014년 11월 19일
how do i say if a row,column == another row,column in the same matrix?
Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 19일
Your comment is not clear
Maybe
if matrix1(row, column) == matrix2(row, column)
??? But like Azzi said, it's not clear what you want. If the matrices are floating point numbers, be sure to read the FAQ.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2014년 11월 19일

댓글:

2014년 11월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by