如何获取数组的镜像(MATLAB)?

发布于 2024-07-13 10:20:11 字数 343 浏览 4 评论 0原文

给定一个数组:

array1 = [1 2 3];

我必须像这样反转它:

array1MirrorImage = [3 2 1];

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

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

是否有更漂亮的解决方案?

Given an array:

array1 = [1 2 3];

I have to reverse it like so:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

极度宠爱 2024-07-20 10:20:11

更新:在较新版本的 MATLAB(R2013b 及更高版本)中,首选使用函数 flip 而不是 flipdim,它具有相同的调用语法:

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


Tomas 有正确的答案。 要添加一点,您还可以使用更通用的 flipdim< /code>

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

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 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

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);

or call rot90:

a = rot90(a, 2);  % Rotates matrix by 180 degrees
回忆那么伤 2024-07-20 10:20:11

另一个简单的解决方案是

b = a(end:-1:1);

您也可以在特定维度上使用它。

b = a(:,end:-1:1); % Flip the columns of a

Another simple solution is

b = a(end:-1:1);

You can use this on a particular dimension, too.

b = a(:,end:-1:1); % Flip the columns of a
娇俏 2024-07-20 10:20:11

您可以使用

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

you can use

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

还如梦归 2024-07-20 10:20:11

使用那个 man:% 到数组的镜像 r=input('insert rows');c=('insert columns')b=size(x); r=b(1);c=b(2); a = 0; y(r,c)=0;
对于 i=1:rn=0; g=ra; a=a+1; 对于 j=1:cn=n+1;d(i,j)=x(g,n); 结尾
结束显示(y)

Use that man:%to array's mirror r=input('insert rows');c=('insert columns')b=size(x); r=b(1);c=b(2); a=0; y(r,c)=0;
for i=1:r n=0; g=r-a; a=a+1; for j=1:c n=n+1;d(i,j)=x(g,n); end
end disp(y)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文