Clear Filters
Clear Filters

How to flip every 5th row and column in matrix

1 view (last 30 days)
For exaple I have
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
11 11 11
12 12 12
and I need to get
1 1 1
2 2 2
3 3 3
6 6 6
5 5 5
4 4 4
7 7 7
8 8 8
9 9 9
12 12 12
11 11 11
10 10 10
If I have matrix 12x3 and I divide it in 4 smaller matrix, I have to flip rows in every other matrix. I hope you got what I need.
  1 Comment
Dennis
Dennis on 4 Jul 2018
A=[1 1 1; 2 2 2; 3 3 3; 4 4 4; 5 5 5; 6 6 6; 7 7 7; 8 8 8; 9 9 9; 10 10 10; 11 11 11; 12 12 12];
for k=0:1
A(k*6+4:k*6+6,1:3)=A(k*6+6:-1:k*6+4,1:3)
end

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 4 Jul 2018
Edited: Stephen23 on 4 Jul 2018
>> M = repmat((1:12).',1,3)
M =
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
11 11 11
12 12 12
>> M([4:6:end,6:6:end],:) = M([6:6:end,4:6:end],:)
M =
1 1 1
2 2 2
3 3 3
6 6 6
5 5 5
4 4 4
7 7 7
8 8 8
9 9 9
12 12 12
11 11 11
10 10 10

More Answers (0)

Community Treasure Hunt

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

Start Hunting!