Shift elements of matrix N times (MATLAB)

조회 수: 3 (최근 30일)
high speed
high speed 2022년 3월 9일
댓글: David Hill 2022년 3월 9일
I have a parameter m and an identity matrix of dimensions m-1*m-1
for example: m=4 so the identity matrix is 3*3
a1=[1 0 0
0 1 0
0 0 1]
I want to shift this matrix m-2 times, to obtain :
a2=[0 1 0
0 0 1
1 0 0]
h3=[0 0 1
1 0 0
0 1 0]
In this case I can use this program:
m=4;
for i=1:m
a1=eye(m-1,m-1);
a2=circshift(h1,[0 1]);
a3=circshift(h2,[0 1]);
end
How can I program that in a general way please (in case if m=100 for example) !

채택된 답변

David Hill
David Hill 2022년 3월 9일
a=circshift(eye(m-1),m-2,2);
  댓글 수: 2
high speed
high speed 2022년 3월 9일
@David Hill In this case I don't get the result that I want.
I got just:
a=[0 0 1
1 0 0
0 1 0]
David Hill
David Hill 2022년 3월 9일
I did not understand that you wanted to save all of the matrices. Below saves all the matrices into a single 3D matrix for any m.
a=zeros(m-1,m-1,m-2);
a(:,:,1)=eye(m-1);
for k=2:m-2
a(:,:,k)=circshift(a(:,:,k-1),1,2)
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by