Algorithm which swaps the columns of a matrix A mx(n*2) without looping?

1 回表示 (過去 30 日間)
MRC
MRC 2014 年 5 月 7 日
回答済み: Andrei Bobrov 2014 年 5 月 7 日
Hi all, I need an algorithm which swaps the columns of a matrix A mx(n*2) without looping in the following way:
%if A=[column 1, column 2, ... column n, column n+1, ... column n*2]
%I want B=[column 1, column n+1, column 2, column n+2, ... column n, column 2*n]
E.g.
A=[1 2 3 4; 5 6 0 9; 1 2 1 8]
B=[1 4 2 3; 5 9 6 0; 1 8 2 1]
Could you help me?

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 5 月 7 日
i1 = reshape(1:size(A,2),[],2)';
B = A(:,i1(:));

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by