Swapping the columns based on matrix even length

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

 채택된 답변

Johannes Fischer
Johannes Fischer 2019년 9월 18일
편집: Johannes Fischer 2019년 9월 18일

0 개 추천

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(:))

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 9월 18일
편집: madhan ravi 2019년 9월 18일

1 개 추천

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

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))))
Thanks Rik :) !

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2019년 9월 18일

댓글:

2019년 9월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by