MATLAB: If statement within for within while loop is not fullfilled but loop is exited

breakfor loopifMATLABwhile

we want to modify our randomization, to let matlab rerandomize the order (1-12) if some conditions are met.
So when the restrictions are met, we want the for loop to break, set z=0 to rerandomize within the while loop.
Only if none of the restrictions are violated, we want to set z=1 so that the randomization would not be executed again.
However this is not working with the following code, after running it a few times, some of the randomizations do not follow our conditions … can anyone help? Thanks in advance
z=0;
while z==0
randStim = stimuli(randperm(size(stimuli,1)/2),:);
randomizeOrder(randomizeOrder(:,4)==1,7)=randStim;
for i=1:10
if randomizeOrder(randomizeOrder(:,7)==i,5) == randomizeOrder(randomizeOrder(:,7)==i+1,5) == randomizeOrder(randomizeOrder(:,7)==i+2,5) || ...
randomizeOrder(randomizeOrder(:,7)==i,2) == randomizeOrder(randomizeOrder(:,7)==i+1,2) == randomizeOrder(randomizeOrder(:,7)==i+2,2) || ...
randomizeOrder(randomizeOrder(:,7)==i,6) == randomizeOrder(randomizeOrder(:,7)==i+1,6) == randomizeOrder(randomizeOrder(:,7)==i+2,6)
z=0;
break
else
z=1;
end
end
end

Best Answer

if randomizeOrder(randomizeOrder(:,7)==i,5) == randomizeOrder(randomizeOrder(:,7)==i+1,5) == randomizeOrder(randomizeOrder(:,7)==i+2,5) || ...
The first randomizeOrder(mask) == randomizeOrder(mask2) executes and returns a vector of logical results. You then compare that vector of logical results to randomizeOrder(mask3) which is unlikely to work.
If you want a comparison along the lines A == B == C meaning that A, B, and C all must be the same, then you need to expand it out, such as A==B & B==C