How to find the order of the power of A matrix?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Muhammad Usman
am 28 Feb. 2023
Bearbeitet: Dyuman Joshi
am 28 Feb. 2023
Hi, I want to calculate the order of the power matrix A. Here order doesn't mean for rows into columns but the power n of the matrix in modulo 2.
For example:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1309590/image.jpeg)
I try to wrote the code, but not getting the desired reults i.e., 3. Here's the code:
A = [1 0 1;1 1 0;1 0 0];
B = A;
i = 0;
for n = 1:5
P = A*B;
if P == eye(3)
i = i+1;
break;
else
if mod(P(:,:),2)==0
B(mod(P(:,:),2)==0) = 0
else
B(mod(P(:,:),2)~=0) = 1
end
P = A*B;
end
i = i+1;
end
disp(i)
Please help me out.
Thanks
0 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 28 Feb. 2023
Bearbeitet: Dyuman Joshi
am 28 Feb. 2023
Use isequal to compare matrices and you can directly calculate modulo by mod, you don't need to assign the values specifically -
A = [1 0 1;1 1 0;1 0 0];
B = A;
i = 0;
for n = 1:5
if isequal(B, eye(3))
i = i+1;
break;
else
sprintf('multiplication')
B = A*B
sprintf('calculating modulo 2')
B = mod(B,2)
end
i = i+1;
end
disp(i)
2 Kommentare
Dyuman Joshi
am 28 Feb. 2023
Bearbeitet: Dyuman Joshi
am 28 Feb. 2023
mod of 2 can only have 2 values, if mod of 2 is not equal to 0 then it is equal to 1.
I have edited my answer, so you can take a look at each step for every iteration.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!