# column shuffling of a matrix

5 views (last 30 days)
shivani on 12 Mar 2015
Edited: Andrei Bobrov on 12 Mar 2015
Please let me know if i want column shuffling instead of rows what changes i must need in this code.
% for the color image, for even rows and cols
% Need to perform switching of rows at each channel
[m, n, c] = size(A);
temp= zeros(m,n);
for i = 1 : c
A1 = A(:, :, i);
a1 = A1(1 : m/2, :);
a2 = A1(end: -1 : m/2+1, :);
temp(1:2:end,:) = a1;
temp(2:2:end,:) = a2;
A(:, :, i) = temp;
end

Andrei Bobrov on 12 Mar 2015
Edited: Andrei Bobrov on 12 Mar 2015
m = size(A);
out = zeros(m);
m1 = round(m(2)/2);
out(:,1:2:end,:) = A(:,1:m1,:);
out(:,2:2:end,:) = A(:,end:-1:m1+1,:);

Michael Haderlein on 12 Mar 2015
Edited: Guillaume on 12 Mar 2015
If this code is sorting the rows in the manner you want, you can change it to do the same with columns this way:
[m, n, c] = size(A);
temp= zeros(m,n);
for i = 1 : c
A1 = A(:, :, i);
a1 = A1(:, 1 : n/2);
a2 = A1(:, end: -1 : n/2+1);
temp(:, 1:2:end) = a1;
temp(:, 2:2:end) = a2;
A(:, :, i) = temp;
end