MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何获得阵列的镜像(MATLAB)? (https://www.labfans.com/bbs/showthread.php?t=22601)

poster 2019-12-10 16:49

如何获得阵列的镜像(MATLAB)?
 
给定一个数组:

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

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

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


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

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


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

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); 或致电[URL="http://www.mathworks.com/help/matlab/ref/rot90.html"]rot90[/URL] :

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

[url=https://stackoverflow.com/questions/501849]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:28

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.