Flipping a matrix diagonally

172 views (last 30 days)
Curtis Lam
Curtis Lam on 7 Jul 2021
Edited: DGM on 12 Sep 2024
I would like to flip a matrix that I have diagonally from left to right as shown in the image. Is there a command or a simple way to do this? The other two ends of my matrices have the correct values so I do not want them to move

Accepted Answer

DGM
DGM on 7 Jul 2021
Edited: DGM on 7 Jul 2021
I'm assuming you want to flip the whole matrix diagonally
To flip about the southeast-northwest diagonal is just transpose:
A = magic(5)
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
A.'
ans = 5×5
17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9
So then flipping about the opposite diagonal is just a matter of flipping one axis:
fliplr(fliplr(A).')
ans = 5×5
9 3 22 16 15 2 21 20 14 8 25 19 13 7 1 18 12 6 5 24 11 10 4 23 17
  3 Comments
DGM
DGM on 8 Jul 2021
I don't see how that's possible with any rigid transformation like a flip/transpose/rotation. The fact that adjacent corners become opposite corners leads me to question what you expect the interior of the array to look like.
Consider:
A = [147 278;
457 13];
becomes
B = [457 147;
278 13];
B is basically A' with the top row flipped. What if there were more rows?
A = [147 156 278;
124 456 583;
457 46 13];
We could transpose and then ...? How do you half-flip a row?
B = [457 124 147;
??? ??? ???;
278 583 13];
I'm not really sure what this transformation is supposed to do. I mean anything is possible with interpolation, but the question is what it means.
John D'Errico
John D'Errico on 12 Sep 2024
Yes. @DGM answered the question as originally posed, which seemed to be about a flip across the anti-diagonal.
But now the question has morphed into one where one corner of the matrix stays the same, but the other three corners become permuted into a new order? This will be a nonlinear thing. And that means what happens inside a larger array will get completely scrambled. While you could surely do something using interpolation, the resulting array will not actually contain any of the original entries in the matrix, except for the 4 main corners. And that seems to be entirely against the concept of a "flip" or transpose.

Sign in to comment.

More Answers (2)

KSSV
KSSV on 7 Jul 2021
A = rand(5) ;
n = size(A,1) ;
v = A(1:n+1:end) ;
A(1:n+1:end) = fliplr(A(1:n+1:end))
Also read about diag.
  1 Comment
Isobel
Isobel on 25 Jan 2023
This flips the diagonal, not the matrix along the diagonal.

Sign in to comment.


Ramdev Rajeshbhai Gohil
Ramdev Rajeshbhai Gohil on 12 Sep 2024
Hi. To flip the matrix diagonally as required by you....it will have to go through the following sequence:
flip-transpose-flip-transpose
or
transpose-flip-transpose-flip
Any of these will work for both square or rectangular matrices.
So the code will be:
A=fliplr((fliplr(A'))');
Thanks.
  1 Comment
DGM
DGM on 12 Sep 2024
Edited: DGM on 12 Sep 2024
Consider the 2x2 example that was given;
% the stated input
A = [147 278
457 13];
% the stated output
B0 = [457 147
278 13];
% this output
B = fliplr((fliplr(A'))')
B = 2×2
13 457 278 147
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
That doesn't match the requested output.
Consider the quadrilateral connecting the array corners ABCD:
The specified output maps these corner positions ABCD to DACB. This folded quadrilateral isn't the product of any number of rigid transformations (rotation, flip, transpose).
Of course, we could do this transformation, but when presented as a 2x2 array, it's not clear what's intended to happen on the interior of the array. Consider the standard cameraman.tif image. We could just pad the rows with some value:
... or we could stretch the rows to fit?
... or we could do any number of discontinuous things to meet the constraints at the corners.
It's hard for me to come up with a scenario where either seems useful.
OP never clarified, but I suspect that the original example is just a mistake. It's anybody's guess what was intended, so you might even be right.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!