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.

6 次查看(过去 30 天)
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.

采纳的回答

Sugandhi
Sugandhi 2023-3-20
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

更多回答(3 个)

Dyuman Joshi
Dyuman Joshi 2023-3-20
编辑:Dyuman Joshi 2023-3-20
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-3-24
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?
Stephen23
Stephen23 2023-3-24
"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.

请先登录,再进行评论。


Matt J
Matt J 2023-3-20
编辑:Matt J 2023-3-20
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

John D'Errico
John D'Errico 2023-3-20
编辑:John D'Errico 2023-3-20
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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by