How to Swap alternate rows of a column matrix
9 views (last 30 days)
Show older comments
I have a arbitary column matrix. Please suggest how to swap alternate rows of the matrix? For example:
A = [r1; r2; r3; r4; r5; r6]
Assume even number of rows. Expected is
A= [r2; r1; r4; r3; r6; r5]
0 Comments
Answers (2)
Andrei Bobrov
on 28 Jan 2016
n = size(A,1);
k = rem(n,2);
out = A(flipud(reshape(1:n-k,2,[])),:);
if k == 1, out = [out;A(end,:)]; end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!