How can I shift rows and columns, and also replicating Sub matrixes.

조회 수: 6 (최근 30일)
Kristopher
Kristopher 2014년 6월 20일
댓글: shivs 2022년 10월 23일
First question, I have a 4x4 magic matrix, A. I want to create a new matrix D by shifting the rows in matrix A down by one, and by shifting the columns left by 2. What would follow D= ?
Second question I have a 2x2 matrix B. I want to create a 4x4 matrix by replicating B 4 times. How would I go about doing that?

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 6월 20일
A=reshape(1:16,4,4) , % First example
A=circshift(A,[1 0]) % Shift down by 1
A=circshift(A,[0 -2]) % Shift left by 2
B=[1 2;3 4] % Second example
B=repmat(B,2,2);

David Sanchez
David Sanchez 2014년 6월 20일
A = magic(4);
D = [A(end,:); A(1:end-1,:)]; % shift rows down
D = [D(:,3:end) D(:,1:2)]; % shift colums leftx2
B = rand(2); %2x2 matrix
G = [B B; B B];%4x4 matrix by replicating B 4 times

Andrei Bobrov
Andrei Bobrov 2014년 6월 20일
A = magic(4);
D = circshift(A,[1,2]);
B = randi(10,2);
out = kron(ones(2),B);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by