MATLAB: How to multiply matrix with vector n times

matrix

hi guys i have this code:
M =[0 1 0 0 0 0 0 0;0 0 1 0 0 0 0 0;0 0 0 1 0 0 0 0;0 0 0 0 1 0 0 0; 0 0 0 0 0 1 0 0;0 0 0 0 0 0 1 0;0 0 0 0 0 0 0 1;1 1 0 0 0 0 0 0];
A = [ 1 0 0 0 0 0 0 0];
C =mod((A*M'),2) % multiplication by module 2
i want to repeat the C =mod((A*M'),2) many times until one of the results repeated. how to do this?

Best Answer

I assume you mean you want to repeat
A = mod(A*M', 2);
Otherwise, if A and M never changes C = mod(A*M', 2) will always give the same result!
Assuming you want to stop when any of the A that's been generated reappers again, build a matrix of A (concatenate rows) and use ismember(..., 'rows') to find if your new A is present in the matrix:
M =[0 1 0 0 0 0 0 0;0 0 1 0 0 0 0 0;0 0 0 1 0 0 0 0;0 0 0 0 1 0 0 0; 0 0 0 0 0 1 0 0;0 0 0 0 0 0 1 0;0 0 0 0 0 0 0 1;1 1 0 0 0 0 0 0];
A = [ 1 0 0 0 0 0 0 0];
AA = A;
A = mod(A*M', 2)
while ~any(ismember(A, AA, 'rows'))
AA = [AA; A];
A = mod(A*M', 2);
end