%array oprations

clear all;
close all;

%size of array
Sx=27; %Width of array
Sy=54; %length of array
ArraySize=[2*Sx+1,2*Sy+1];
Time=3; %we run the simulation for this long
deltat=1; %we will show one iteration for this long

Array=round(rand(ArraySize)*255);


Processors=ones(ArraySize)*255;
for a=1:Sx
	for b=1:Sy
		Processors(2*a,2*b)=0;
	end
end
%show processors
%figure
%image(Processors);




figure
h = imagesc(Array); set(h,'erasemode','xor'); 
colormap gray;


for t=1:Time
	%random execution order of the blocks
	%delete this if it does not work in the lab
	XOrder=randperm(Sx);
	YOrder=randperm(Sy);
	for a=1:Sx
		for b=1:Sy
			XIndex=XOrder(a);
			YIndex=YOrder(b);
			%get neighbourhood
			%local memory of the current processor
			LocalMem=Array((2*XIndex-1):(2*XIndex+1),(2*YIndex-1):(2*YIndex+1));
			%we have to write this function
			LocalMem=OperationMax(LocalMem,t);
			%LocalMem=OperationOrdering(LocalMem,t);
			Array((2*XIndex-1):(2*XIndex+1),(2*YIndex-1):(2*YIndex+1))=LocalMem;
		end
	end
	
	title(sprintf('%d(/%d). iteárció',t,Time)) 
	pause(deltat)
	set(h,'cdata',Array);
	colormap gray;
end
