フィルターのクリア

Switch columns in a matrix without a loop

1 回表示 (過去 30 日間)
yakov
yakov 2013 年 4 月 13 日
コメント済み: sami elahj 2016 年 1 月 10 日
How can I switch specific columns in a matrix (for example every 4th column), in one command without using a loop? Is it even possible?

回答 (2 件)

Florian
Florian 2013 年 4 月 13 日
Hi Yakov,
please specify, since in a matrix with 10 columns you will have for 3 columns at index 3,6,9 but only 2 columns with index mod 5, namely 5, 10.
Hence could you please specify exactly which columns you want to switch. Or do you want to mix all columns randomly?
Regards, Florian
  1 件のコメント
yakov
yakov 2013 年 4 月 13 日
Hi Florian, for example, if I choose to switch every third column, in a matrix of 10 columns I'd want to switch the 3,6,9 columns only, and if I choose to switch every fourth column I'd want to switch the 4 and 8 columns. I want to switch them with a different column vector of my choosing, but all of them with the same vector.

サインインしてコメントする。


Ahmed A. Selman
Ahmed A. Selman 2013 年 4 月 13 日
for exchanging two columns 3 - 9 :
A = randi(20,4,10); % A random 4 by 10 matrix of integers
A(1,:)=[1 2 3 4 5 6 7 8 9 10]; % the first row only is replaced for clarity
B=A; % the new matrix, same as A for now
B(:,3)=A(:,9); % puts 3 of B with 9 of A
B(:,9)=A(:,3); % puts 9 B with 3 A
B has columns 3 and 9 exchanged with A 9 and 3, and A is intact. A similar method to exchange columns 3 - 6 - 9 - 3. So if you want to use same A, use A=B.
To exchange rows use B(row_no,:)=A(new_row,:)..etc
  1 件のコメント
sami elahj
sami elahj 2016 年 1 月 10 日
thanx

サインインしてコメントする。

カテゴリ

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