How to mirror matrix on the diagonal?

157 ビュー (過去 30 日間)
SL
SL 2016 年 10 月 21 日
編集済み: DGM 2022 年 8 月 3 日
I want to mirror data matrix on the diagonal.
Input:
y
|
____ x
Expected output:
x
|_y
I know you these transformations but I cannot get mirror around the diagonal (y=x line from (0,0) to (1,1))
I = imread('onion.png');
I2 = flipdim(I ,2); %# horizontal flip
I3 = flipdim(I ,1); %# vertical flip
I4 = flipdim(I3,2); %# horizontal+vertical flip
MATLAB: 2016b OS: Debian 8.5
  2 件のコメント
Massimo Zanetti
Massimo Zanetti 2016 年 10 月 21 日
Can you give a simple example with numeric array of the "mirroring" you need? Is it something like:
1 2 3
4 5 6
7 8 9
to
9 6 3
8 5 2
7 4 1
?
SL
SL 2016 年 10 月 21 日
Yes, your example is valid.

サインインしてコメントする。

採用された回答

Thorsten
Thorsten 2016 年 10 月 21 日
I2 = rot90(fliplr(I),-1);
  3 件のコメント
Emmanuel Atoleya Atindama
Emmanuel Atoleya Atindama 2022 年 8 月 3 日
Yes. This works on n-d arrays. The other answers involving transpose do not work in arrays greater than 2 dimensions, unless you want to iterate thru the other dimensions.
DGM
DGM 2022 年 8 月 3 日
To add to the confusion, depending on what version you use, this may still not work with arrays that are more than 2D. In older versions, rot90(), fliplr() and flipud() are limited to work only on 2D arrays. It might not be that relevant today, but this is not a current thread.

サインインしてコメントする。

その他の回答 (2 件)

Massimo Zanetti
Massimo Zanetti 2016 年 10 月 21 日
編集済み: Massimo Zanetti 2016 年 10 月 21 日
In the case described before it is:
A=[1 2 3;4 5 6;7 8 9]
rot90(A,2)'
which gives:
A= 1 2 3
4 5 6
7 8 9
to
9 6 3
8 5 2
7 4 1
  3 件のコメント
Massimo Zanetti
Massimo Zanetti 2016 年 10 月 22 日
I can't see why it shouldn't work... You must give me an example, otherwise I cannot understand. No problems with rectangular matrices.
DGM
DGM 2022 年 8 月 3 日
編集済み: DGM 2022 年 8 月 3 日
The transpose operator doesn't work on anything other than a 2D array, but you can still use permute().
A = repmat([1 2 3;4 5 6;7 8 9],[1 1 3])
A =
A(:,:,1) = 1 2 3 4 5 6 7 8 9 A(:,:,2) = 1 2 3 4 5 6 7 8 9 A(:,:,3) = 1 2 3 4 5 6 7 8 9
B = permute(rot90(A,2),[2 1 3]) % use permute()
B =
B(:,:,1) = 9 6 3 8 5 2 7 4 1 B(:,:,2) = 9 6 3 8 5 2 7 4 1 B(:,:,3) = 9 6 3 8 5 2 7 4 1
C = pagetranspose(rot90(A,2)) % or use pagetranspose() (R2020b or newer)
C =
C(:,:,1) = 9 6 3 8 5 2 7 4 1 C(:,:,2) = 9 6 3 8 5 2 7 4 1 C(:,:,3) = 9 6 3 8 5 2 7 4 1
That said, if the OP isn't aware of the array dimensionality, then there are probably other problems.

サインインしてコメントする。


Fady Samann
Fady Samann 2020 年 8 月 13 日
you can do the following:
first, transpose the matrix
A = table.';
Flip it horizontally
A = flip (A,1);
then, flip it verticaly
A = flip (A,2);

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by