Swapping the columns based on matrix even length

1 visualización (últimos 30 días)
Venkat Ta
Venkat Ta el 18 de Sept. de 2019
Comentada: madhan ravi el 18 de Sept. de 2019
Is there any pre-function in Matlab to change the column order below like it?
Original column orders = 1 2 3 4 5 6 7 8 9 10
Modified coulmn orders = 2 1 4 3 6 5 8 7 10 9
cell format and matrix format how can it change?
Thanks

Respuesta aceptada

Johannes Fischer
Johannes Fischer el 18 de Sept. de 2019
Editada: Johannes Fischer el 18 de Sept. de 2019
Assuming that there is always an even number of columns:
matrix = repmat(1:10, [3, 1]);
L = size(matrix, 2);
ind = [2:2:L; 1:2:L];
matrix_reorderedColumns = matrix(:, ind(:))

Más respuestas (1)

madhan ravi
madhan ravi el 18 de Sept. de 2019
Editada: madhan ravi el 18 de Sept. de 2019
No matter odd or even:
ix=(2:2:size(matrix,2)) + [0;-1];
% if > 2016b ix = bsxfun(@plus, 2:2:size(a,2), [0;-1])
Wanted = matrix(:,[ix(:);end.*(mod(end,2)~=0)])
  2 comentarios
Rik
Rik el 18 de Sept. de 2019
Or with a few small tweaks:
N=10;%set to something odd or even to check both
matrix = repmat(1:N, [3, 1]);
ix=(2:2:(size(matrix,2)+1)) + [0;-1];
% if > 2016b ix = bsxfun(@plus, 2:2:size(a,2), [0;-1])
Wanted = matrix(:,ix(ismember(ix,1:size(matrix,2))))
madhan ravi
madhan ravi el 18 de Sept. de 2019
Thanks Rik :) !

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by