# How to build a function fun that takes as input the matrix A and as output provides the matrix B which is produced out of A in the following ways.

3 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Sugandhi
on 20 Mar 2023

Hi Omorodion Solomon,

I understand that you want to build a function that takes matrix A as input and gives matrix B as output , where B is formed after arranging A.

You can solve this as given below:

a=[1 2 3 4;5 6 7 8;9 10 11 12; 13 14 15 16]

n=length(a);

halfn=round(n/2);

a1=a(1:halfn,1:halfn);

a2=a(1:halfn,halfn+1:n);

a3=a(halfn+1:n,1:halfn);

a4=a(halfn+1:n,halfn+1:n);

b=[a4,a3;a2,a1]

##### 0 Comments

### More Answers (3)

Dyuman Joshi
on 20 Mar 2023

Edited: Dyuman Joshi
on 20 Mar 2023

A much simpler way -

a=[1 2 3 4;5 6 7 8;9 10 11 12; 13 14 15 16]

s=size(a)/2;

b=circshift(a,s)

##### 5 Comments

Stephen23
on 24 Mar 2023

"Is there any information about how exactly the header works or how the modification happens? "

I suspect that the behavior and content of the array header is rather proprietary and subject to change. Usually TMW advises to avoid writing code that relies on such implementation details.

With that in mind, you can find a few clues in the MATLAB documentation:

You can probably find more information in the MXARRAY documentation somewhere.

John D'Errico
on 20 Mar 2023

Edited: John D'Errico
on 20 Mar 2023

Possibly homework. (I never trust simple problems like this, where the goal is simply to move thigns around in a matrix. They are almost always homework assignments.) But you already have two answers, and you have accepted one. So I might as well show the pretty way to solve the problem. You can do it via matrix multiplies. I've built it to perform the swaps using sparse matrices, to make it efficient for large matrices.

The trick is to pre-multiply and post-multiply by a sparsely constructed permutation matrix.

A = reshape(1:16,4,4)'

A = swapblocks(A)

A = rand(5000);

timeit(@() swapblocks(A))

And that seems reasonably fast for a 5kx5k matrix permutation.

Do you see how it works? For the 4x4 mtrix, here is how it works. Constriuct a 4x4 transformation matrix, that looks like this:

T = [zeros(2),eye(2);eye(2),zeros(2)]

See what happens when we multiply T*A.

A = reshape(1:16,4,4)'

T*A

As you can see, that swaps the blocks from top to bottom. Essentially, it swaps the rows to go from [1 2 3 4] to [3 4 1 2]. Next, when we post multiply by T, it swaps the columns, in the same blockwise fashion.

T*A*T

But of course, swapblocks was written to use a sparse matrix T. That maks the matrix multiplies far more efficient when A is large.

function Aswap = swapblocks(A)

% swaps the 4 corner n by n blocks of a 2*n by 2*n matrix from [1 2;3 4] to [4 3;2 1]

% usage: Aswap = swapblocks(A)

%

% arguments:

% A - any 2n x 2n matrix

% what size is A?

[N,M] = size(A);

if (N~=M) || (mod(N,2)==1)

error('A must be a square matrix with an even number of rows and columns')

end

% build the tranform matrix

n = N/2;

T = speye(n);

S = spalloc(n,n,0);

T = [S,T;T,S];

% the transformation is a simple one

Aswap = T*A*T;

end

Anyway, if this was homework, your teacher will never believe you solved it yourself like this.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!