# Processing random matrices

I am doing my research about scheduling by using Matlab. I am a new Matlab user and I have a problem with time execution of my following code:

clc
clear all
A=8;
B=12;
C=10;
ProcessTime= [ 11 11 11 11 4 4 4 4 4 4 4 2 ]; %Converting Matrice

RandomMatriceA=zeros(A,B,C);
RandomMatriceB=zeros(A,B,C);
SumRandomMatriceB=zeros(1,A,C);
ConvertMatriceA=zeros(A,B,C);
ProcessRandomMatriceA=zeros(A,B,C);
StartProcess=zeros(A,B,C);
EndProcess=zeros(A,B,C);
StartPost=zeros(A,B,C);

for ii=1:C;
for x=1:2:A-1;
%make first random matrice
[vals RandomMatriceA(x,:,ii)]=sort(rand(1,B),2); %batasan tidak boleh satu kelompok melakukan lebih dari satu aktivitas dalam satu waktu
done=false;
while ~done,
%make row of second random matrice
NewRowRandomMatriceB=randi([0 2],1,B);
%Make sure sum all element per row in RandomMatriceB <=11
done=sum(NewRowRandomMatriceB)<12;
end
RandomMatriceB(x,:,ii)=NewRowRandomMatriceB;
%After making RandomMatriceA and RandomMatriceB, then Make New
%Matrice
%To know varible of new matrice which is result of combining
%RandomMatriceA,RandomMatriceB and ProcessTime
for y=1:B,
ConvertMatriceA(x,y,ii)=ProcessTime(RandomMatriceA(x,y,ii));%FirstVarible:Consecutive The Number of value in all element of RandomMatriceA
end
ProcessRandomMatriceA(x,:,ii)=ProcessTime(RandomMatriceA(x,:,ii))+RandomMatriceB(x,:,ii);
EndProcess(x,:,ii)=cumsum(ProcessRandomMatriceA(x,:,ii),2);%secondVaribale:to know in which column The Consecutive value RandomMatrice will be end
StartProcess(x,:,ii)=EndProcess(x,:,ii)-(ProcessTime(RandomMatriceA(x,:,ii))-1);%ThirdVariable:to know in which column The Consecutive Value will be start
for yy=1:B;
N=RandomMatriceA(x,yy,ii);
StartPost(x,N,ii)=StartProcess(x,yy,ii);
end
end
for h=2:2:A;
doneA=false;
while ~doneA,
[vals RandomMatriceA(h,:,ii)]=sort(rand(1,B),2);
doneB=false;
while ~doneB,
NewRowRandomMatriceB=randi([0 2],1,B);
doneB= sum(NewRowRandomMatriceB)<12;
end
RandomMatriceB(h,:,ii)=NewRowRandomMatriceB;
for y=1:B;
ConvertMatriceA(h,y,ii)=ProcessTime(RandomMatriceA(h,y,ii));
end
ProcessRandomMatriceA(h,:,ii)=ProcessTime(RandomMatriceA(h,:,ii))+RandomMatriceB(h,:,ii);
EndProcess(h,:,ii)=cumsum(ProcessRandomMatriceA(h,:,ii),2);%secondVaribale:to know in which column The Consecutive value RandomMatrice will be end
StartProcess(h,:,ii)=EndProcess(h,:,ii)-(ProcessTime(RandomMatriceA(h,:,ii))-1);%ThirdVariable:to know in which column The Consecutive Value will be start
for z=1:B;
N=RandomMatriceA(h,z,ii);
StartPost(h,N,ii)=StartProcess(h,z,ii);
end
doneA =all(ismember(StartPost(h,1:4,ii),StartPost(h-1,1:4,ii)));
end
%arrange value in column 1 until 4 in order that it will have same
%value
StartPost(h,1:4,ii)=StartPost(h-1,1:4,ii);
end
end


After using the profile viewer, I know that my problem is in this following code. This part takes too much execution time.

....
end
doneA =all(ismember(StartPost(h,1:4,ii),StartPost(h-1,1:4,ii)));
end
%arrange value in column 1 until 4 in order that it will have same
%value
StartPost(h,1:4,ii)=StartPost(h-1,1:4,ii);
end


How can I improve it and make it run faster?