tags:

views:

5257

answers:

3

Problem:

array1 = [1 2 3];

must be converted in

array1MirrorImage = [3 2 1];

So far I obtained the ugly solution below:

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

There must be a prettier solution to this. Anyone knows one?

+7  A: 

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(length(gen):-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

Tomas Lycken
+8  A: 

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

EDIT: 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
gnovice
Re: your edit, you can also just use `b = a(end:-1:1);` to flip ALL dimensions of a matrix.
Scottie T
One caveat to that option is that the matrix appears to get reshaped into a 1-by-length(a) vector, so you would have to call RESHAPE afterwards. This may be version specific (I am running MATLAB ver. 7.1).
gnovice
Ah, that's right. You'd have to use reshape.
Scottie T
+4  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
Scottie T
Good point. END makes things cleaner and easier to read by removing arguments like "length(a)".
gnovice