PDA

查看完整版本 : 如何获得阵列的镜像(MATLAB)?


poster
2019-12-10, 16:49
给定一个数组:

array1 = [1 2 3]; 我必须像这样扭转它:

array1MirrorImage = [3 2 1]; 到目前为止,我获得了这个丑陋的解决方案:

array1MirrorImage = padarray(array1, [0 length(array1)], 'symmetric', 'pre'); array1MirrorImage = array1MirrorImage(1:length(array1)); 有更漂亮的解决方案吗?


回答:
更新:在较新版本的MATLAB(R2013b及更高版本)中,首选使用功能flip (http://www.mathworks.com/help/matlab/ref/flip.html)而不是flipdim (http://www.mathworks.com/help/matlab/ref/flipdim.html) ,后者具有相同的调用语法:

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


Tomas有正确的答案。要添加一点,您还可以使用更通用的flipdim (http://www.mathworks.com/help/matlab/ref/flipdim.html) :

a = flipdim(a, 1); % Flips the rows of a a = flipdim(a, 2); % Flips the columns of a 一个额外的小技巧...如果出于某种原因必须翻转二维数组的两个维度,则可以调用flipdim两次:

a = flipdim(flipdim(a, 1), 2); 或致电rot90 (http://www.mathworks.com/help/matlab/ref/rot90.html) :

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

更多&回答... (https://stackoverflow.com/questions/501849)