Help needed adding features to this script.

I have a script (shown below) that will generate a 12x12 matrix of all 0's and a single 1, which will replicate in a randomised order 7200 times. There are three features I want to add to this script but don't know how:
  1. Instead of generating a matrix in a complete random order I want digits to move either one place up, down, left or right from their position in the previous matrix.
  2. A shortcut that will create an imagesc() for all 7200 matrix's (like the image below).
Any help would be greatly appreciated.
Thanks,
Joe
count=0;
m=zeros(144,1);
rand_int=datasample(1:144,1);
m(rand_int)=1;
old_matrix=reshape(m,[12 12]);
for i=1:7200
m=zeros(144,1);
rand_int=datasample(1:144,1);
m(rand_int)=1;
new_matrix=reshape(m,[12 12]);
if sum(abs(old_matrix-new_matrix),'All')>0
count=count+1;
end
old_matrix=new_matrix
end

 采纳的回答

Here is one way:
m = zeros(12, 12);
numIterations = 7200;
row = ones(1, numIterations);
col = ones(1, numIterations);
m(row(1), col(1)) = 1;
imagesc(m);
axis('on', 'image');
for k = 2 : numIterations
row(k) = row(k-1) + randi([-1,1], 1);
while row(k) <= 0 || row(k) > size(m, 1)
row(k) = row(k-1) + randi([-1,1], 1);
end
col(k) = col(k-1) + randi([-1,1], 1);
while col(k) <= 0 || col(k) > size(m, 2)
col(k) = col(k-1) + randi([-1,1], 1);
end
m(row(k), col(k)) = 1; % Set new location
m(row(k-1), col(k-1)) = 0; % Clear old location
imagesc(m);
drawnow;
end

9 个评论

That works great thanks!
Can you think of any way to slow down the image creation? a delay of sorts.
Put in a pause(0.5) to pause the display some time.
m = zeros(12, 12);
numIterations = 7200;
row = ones(1, numIterations);
col = ones(1, numIterations);
m(row(1), col(1)) = 1;
imagesc(m);
axis('on', 'image');
for k = 2 : numIterations
row(k) = row(k-1) + randi([-1,1], 1);
while row(k) <= 0 || row(k) > size(m, 1)
row(k) = row(k-1) + randi([-1,1], 1);
end
col(k) = col(k-1) + randi([-1,1], 1);
while col(k) <= 0 || col(k) > size(m, 2)
col(k) = col(k-1) + randi([-1,1], 1);
end
m(row(k), col(k)) = 1; % Set new location
m(row(k-1), col(k-1)) = 0; % Clear old location
imagesc(m);
drawnow;
pause(0.5);
end
Great thanks!
Also could you tell me why the image will periodically turn a light blue colour and then back. Is this a processing issue due to the number of images being created?
I didn't see that. Occasionally I'd see the screen flicker. Maybe it picks a colormap that's different if the array is all zeros with no ones. You might reverse the clearing and setting to make sure that there is always a 1 there in case it picked the same location:
Change
m(row(k), col(k)) = 1; % Set new location
m(row(k-1), col(k-1)) = 0; % Clear old location
to
m(row(k-1), col(k-1)) = 0; % Clear old location
m(row(k), col(k)) = 1; % Set new location
Also one final question, is there a way of increasing the percentage chance that the 1 in the matrix wont change position when the matrix replicates? maybe a 1:2 ratio.
Again thanks for your speedy responses.
Joe
Just skip the loop if the probability is less. And if trying a new place, make sure it's not the same as the old place.
m = zeros(12, 12);
numIterations = 7200;
row = ones(1, numIterations);
col = ones(1, numIterations);
m(row(1), col(1)) = 1;
imagesc(m);
axis('on', 'image');
percentage = 0.20; % 20% of time will be the same
for k = 2 : numIterations
if rand(1) < percentage
% 20% of the time it will stay in the same place.
row(k) = row(k-1);
col(k) = col(k-1);
else
% 80% of the time it will select a new place.
% First get a tentative new location.
row(k) = row(k-1) + randi([-1,1], 1);
col(k) = col(k-1) + randi([-1,1], 1);
while row(k) == row(k-1) && col(k) == col(k-1)
while row(k) <= 0 || row(k) > size(m, 1)
row(k) = row(k-1) + randi([-1,1], 1);
end
while col(k) <= 0 || col(k) > size(m, 2)
col(k) = col(k-1) + randi([-1,1], 1);
end
end
end
m(row(k-1), col(k-1)) = 0; % Clear old location
m(row(k), col(k)) = 1; % Set new location
imagesc(m);
grid on;
caption = sprintf('At Iteration %d, Row = %d, Column = %d', k, row(k), col(k));
title(caption, 'FontSize', 15);
drawnow;
pause(0.4)
end
I get an error message when I run it and the matrix doesn't change
Sorry, try this (actually tested this time):
m = zeros(12, 12);
numIterations = 7200;
row = ones(1, numIterations);
col = ones(1, numIterations);
m(row(1), col(1)) = 1;
imagesc(m);
axis('on', 'image');
percentage = 0.20; % 20% of time will be the same
for k = 2 : numIterations
if rand(1) < percentage
% 20% of the time it will stay in the same place.
row(k) = row(k-1);
col(k) = col(k-1);
else
% 80% of the time it will select a new place.
% First get a tentative new location.
row(k) = row(k-1) + randi([-1,1], 1);
col(k) = col(k-1) + randi([-1,1], 1);
while row(k) == row(k-1) && col(k) == col(k-1) || row(k) <= 0 || row(k) > size(m, 1) || col(k) <= 0 || col(k) > size(m, 2)
% Get a new location.
row(k) = row(k-1) + randi([-1,1], 1);
col(k) = col(k-1) + randi([-1,1], 1);
while row(k) <= 0 || row(k) > size(m, 1)
row(k) = row(k-1) + randi([-1,1], 1);
end
while col(k) <= 0 || col(k) > size(m, 2)
col(k) = col(k-1) + randi([-1,1], 1);
end
end
end
m(row(k-1), col(k-1)) = 0; % Clear old location
m(row(k), col(k)) = 1; % Set new location
imagesc(m);
grid on;
caption = sprintf('At Iteration %d, Row = %d, Column = %d', k, row(k), col(k));
title(caption, 'FontSize', 15);
drawnow;
% pause(0.4)
end
Great thanks for your help.
Could you tell me how to assign colors to the numbers. white for zero, red for one? Is this possible to do using Colormaps?

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by