给定一个数组:
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
托马斯有正确的答案.要添加一点,您还可以使用更一般的flipdim
:
a = flipdim(a, 1); % Flips the rows of a a = flipdim(a, 2); % Flips the columns of a
另外一个小技巧......如果由于某种原因你必须翻转2-D数组的两个尺寸,你可以调用flipdim
两次:
a = flipdim(flipdim(a, 1), 2);
或致电rot90
:
a = rot90(a, 2); % Rotates matrix by 180 degrees
另一个简单的解决方案
b = a(end:-1:1);
您也可以在特定维度上使用它.
b = a(:,end:-1:1); % Flip the columns of a
您可以使用
rowreverse = fliplr(row) % for a row vector (or each row of a 2D array) colreverse = flipud(col) % for a column vector (or each column of a 2D array) genreverse = x(end:-1:1) % for the general case of a 1D vector (either row or column)
http://www.eng-tips.com/viewthread.cfm?qid=149926&page=5