how do I flip ever column in a matrix without using the fliplr function?

n = randi([4,15]);
n_mat = randi ( [1,100], n, n)
this generates a matrix, and I want basically do the "fliplr" command with just operations

 Accepted Answer

n = randi([4,15]);
n_mat = randi ( [1,100], n, n)
n_mat = 6×6
8 27 54 5 24 28 86 65 85 87 30 81 21 47 87 93 29 33 18 84 9 44 42 68 40 67 83 92 87 95 63 2 87 95 68 35
n_mat_flipped = n_mat(:,end:-1:1)
n_mat_flipped = 6×6
28 24 5 54 27 8 81 30 87 85 65 86 33 29 93 87 47 21 68 42 44 9 84 18 95 87 92 83 67 40 35 68 95 87 2 63

3 Comments

n_mat(:,end:-1:1)
is(entire row, end column : order -1 : to the first element?)
Precisely! ( -1 to go in reverse)
No. Colon : means "All rows" rather than an entire row. So it's reversing each and every row, putting the last column first, then the next to the last column, and finally on the right edge it will be the original first element in the row. Again that's done for all rows so all columns are flipped left to right..

Sign in to comment.

More Answers (0)

Categories

Asked:

on 18 May 2022

Commented:

on 18 May 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!