필터 지우기
필터 지우기

How to find the order of the power of A matrix?

조회 수: 2 (최근 30일)
Muhammad Usman
Muhammad Usman 2023년 2월 28일
편집: Dyuman Joshi 2023년 2월 28일
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:
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
B = 3×3
1 0 1 1 1 1 1 0 1
B = 3×3
1 0 1 1 1 1 1 0 1
B = 3×3
1 0 1 1 1 1 1 0 1
B = 3×3
1 0 1 1 1 1 1 0 1
B = 3×3
1 0 1 1 1 1 1 0 1
disp(i)
5
Please help me out.
Thanks

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 2월 28일
편집: Dyuman Joshi 2023년 2월 28일
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
ans = 'multiplication'
B = 3×3
2 0 1 2 1 1 1 0 1
ans = 'calculating mod'
B = 3×3
0 0 1 0 1 1 1 0 1
ans = 'multiplication'
B = 3×3
1 0 2 0 1 2 0 0 1
ans = 'calculating mod'
B = 3×3
1 0 0 0 1 0 0 0 1
disp(i)
3
  댓글 수: 2
Muhammad Usman
Muhammad Usman 2023년 2월 28일
I also need to this condition
B(mod(P(:,:),2)~=0) = 1
that when mod of 2 not equal to zero, replace the entry by 1
Dyuman Joshi
Dyuman Joshi 2023년 2월 28일
편집: Dyuman Joshi 2023년 2월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2023년 2월 28일
편집: Stephen23 2023년 2월 28일
A = [1,0,1;1,1,0;1,0,0];
B = A;
for n = 2:5
B = mod(B*A,2);
if isequal(B,eye(3))
break
end
end
n
n = 3
B
B = 3×3
1 0 0 0 1 0 0 0 1

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by