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.

First A, which is of dimension n, is split into four submatrices of dimension n/2 times n/2. Then the sub matrix in the bottom right coner is switched with the submatrix in the top left corner, whereas the submatrix in the bottom left coner is switched with the submatrix in the top right coner. This is the B matrix.

 Accepted Answer

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]
a = 4×4
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]
b = 4×4
11 12 9 10 15 16 13 14 3 4 1 2 7 8 5 6

More Answers (3)

A much simpler way -
a=[1 2 3 4;5 6 7 8;9 10 11 12; 13 14 15 16]
a = 4×4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
s=size(a)/2;
b=circshift(a,s)
b = 4×4
11 12 9 10 15 16 13 14 3 4 1 2 7 8 5 6

5 Comments

Cute. I had never thought about that capability of circshift. And, on a 5kx5k matrix, it is 3 times faster than my solution, which used a block sparse matrix pre and post multiply.
I read on a thread here that MATLAB is quite efficient with operations involving shifting/moving the elements of matrix around (with that particular comment being about reshape() ) and as it struck to me, I tried a few things out and found this solution.
Though I am pleasantly surprised to know this particular factoid.
"I read on a thread here that MATLAB is quite efficient with operations involving shifting/moving the elements of matrix around (with that particular comment being about reshape() ) and as it struck to me,"
Correction: RESHAPE is very fast precisely because it does not move any data around in memory, it just modifies the very small property header that is attached to the array. That is why RESHAPE specifically is very efficient:
In contrast, all(?) other functions (like CIRCSHIFT) do move data around in memory, which takes (relatively speaking) much more time. So the comment that you read likely only applies to RESHAPE, not to any other function. Moving data around always takes more time than not moving any data around.
Still a very neat answer (which I also voted for).
Thanks for the correction and for the resources,8 @Stephen23.
I remember going through the last thread you linked. Did you find an answer to your question posted as a comment on the same question?
Additionally, Is there any information about how exactly the header works or how the modification happens?
"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.

Sign in to comment.

You can use this FEX download,
Example:
>> A=reshape(1:16,4,4), n=length(A)/2;
A =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
>> B=blkReshape(A,[n,n],1,1,[]);
>> B=blkReshape(B(:,:,[4,3,2,1]),[2,2],[2,2])
B =
11 15 3 7
12 16 4 8
9 13 1 5
10 14 2 6

1 Comment

I think you forgot to switch the submatrix in the botton left conner with that of the top right conner. Thanks alot. i've made the corrections.

Sign in to comment.

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 = 4×4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A = swapblocks(A)
A = 4×4
11 12 9 10 15 16 13 14 3 4 1 2 7 8 5 6
A = rand(5000);
timeit(@() swapblocks(A))
ans = 0.1926
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)]
T = 4×4
0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0
See what happens when we multiply T*A.
A = reshape(1:16,4,4)'
A = 4×4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
T*A
ans = 4×4
9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8
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
ans = 4×4
11 12 9 10 15 16 13 14 3 4 1 2 7 8 5 6
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.

Tags

Community Treasure Hunt

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

Start Hunting!