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 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Omorodion Solomon
il 20 Mar 2023
Commentato: Stephen23
il 24 Mar 2023
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.
0 Commenti
Risposta accettata
Sugandhi
il 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 Commenti
Più risposte (3)
Dyuman Joshi
il 20 Mar 2023
Modificato: Dyuman Joshi
il 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 Commenti
Dyuman Joshi
il 24 Mar 2023
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
il 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
il 20 Mar 2023
Modificato: John D'Errico
il 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 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!