给定一个数组:
array1 = [1 2 3];
我必须像这样反转它:
array1MirrorImage = [3 2 1];
到目前为止,我得到了这个丑陋的解决方案:
array1MirrorImage = padarray(array1, [0 length(array1)], 'symmetric', 'pre');
array1MirrorImage = array1MirrorImage(1:length(array1));
有更好的解决方案吗?
最佳答案
更新:在较新版本的 MATLAB(R2013b 及之后)中,首选使用函数 flip
而不是 flipdim
,具有相同的调用语法:
a = flip(a, 1); % Reverses elements in each column
a = flip(a, 2); % Reverses elements in each row
Tomas 的答案是正确的。要添加一点,您还可以使用更通用的 flipdim
:
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
:
a = rot90(a, 2); % Rotates matrix by 180 degrees
关于arrays - 如何获取数组的镜像(MATLAB)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/501849/