如何获得阵列的镜像(MATLAB)? [英] How to obtain the mirror image of an array (MATLAB)?

查看:86
本文介绍了如何获得阵列的镜像(MATLAB)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个数组:

array1 = [1 2 3];

我必须像这样扭转它:

array1MirrorImage = [3 2 1];

到目前为止,我已经获得了这个丑陋的解决方案:

So far I obtained this ugly solution:

array1MirrorImage = padarray(array1, [0 length(array1)], 'symmetric', 'pre');
array1MirrorImage = array1MirrorImage(1:length(array1));

有没有更漂亮的解决方案?

Is there a prettier solution to this?

推荐答案

更新:在较新版本的MATLAB(R2013b及更高版本)中,首选使用函数

UPDATE: In newer versions of MATLAB (R2013b and after) it is preferred to use the function flip instead of flipdim, which has the same calling syntax:

a = flip(a, 1);  % Reverses elements in each column
a = flip(a, 2);  % Reverses elements in each row


Tomas的答案正确.要添加一点,您还可以使用更通用的 flipdim :

Tomas has the right answer. To add just a little, you can also use the more general flipdim:

a = flipdim(a, 1);  % Flips the rows of a
a = flipdim(a, 2);  % Flips the columns of a

另一个小技巧...如果出于某种原因必须翻转二维数组的两个维,则可以两次调用 flipdim :

An additional little trick... if for whatever reason you have to flip BOTH dimensions of a 2-D array, you can either call flipdim twice:

a = flipdim(flipdim(a, 1), 2);

或致电 rot90 :

a = rot90(a, 2);  % Rotates matrix by 180 degrees

这篇关于如何获得阵列的镜像(MATLAB)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆