我认为这是您需要的,在确定随机排列之前的检查条件?
matrix = [11,22,33,44,55,66,77,88,99];
randOrder = zeros(length(matrix));
randOrderIntermediate = zeros(length(matrix));
randOrderPrev = zeros(length(matrix));
for i = 1:10
%Store the previous random order
randOrderPrev = randOrder;
%Create interim random order
randOrderIntermediate = randperm(length(matrix));
%check condition, is the first the same as the previous end?
while randOrderIntermediate(end) == randOrderPrev(1)
%whilst condition true, re-randomise
randOrderIntermediate = randperm(length(matrix));
end
%since condition is no longer true, set the new random order to be the
%intermediate one
randOrder = randOrderIntermediate;
%As with your original code.
for n = randOrder
disp(matrix(n))
end
end