21

与えられた配列:

array1 = [1 2 3];

次のように逆にする必要があります。

array1MirrorImage = [3 2 1];

これまでのところ、私はこの醜い解決策を得ました:

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

これに対するより良い解決策はありますか?

4

4 に答える 4

31

更新: 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 配列の両方の次元を反転する必要がある場合は、次のいずれかをflipdim2 回呼び出すことができます。

a = flipdim(flipdim(a, 1), 2);

または電話rot90

a = rot90(a, 2);  % Rotates matrix by 180 degrees
于 2009-02-02T00:25:12.230 に答える
20

別の簡単な解決策は

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

これは、特定のディメンションでも使用できます。

b = a(:,end:-1:1); % Flip the columns of a
于 2009-02-04T17:34:28.267 に答える
14

あなたが使用することができます

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

于 2009-02-02T00:10:32.570 に答える