フィルターのクリア

changing arrays of a matrix

1 回表示 (過去 30 日間)
ss
ss 2012 年 12 月 27 日
Hi
if we have a matrix and we want to change the position of its array, rows and columns simultaneously, using two index vector B1 and B2, how can we use this command, i.e for instance u=[u11 u12 u13 u14;u21 u22 u23 u24;u31 u32 u33 u34;u41 u42 u43 u44]; B1=[1 4 3 2] B2=[3 2 1 4](B1 and B2 themselves are columns of a matrix) such that B1 is used for changing rows and B2 for changing columns, so u would be u=[u13 u12 u11 u14;u43 u42 u41 u44;u33 u32 u31 u34; u23 u22 u21 u24] thanks

採用された回答

Jan
Jan 2012 年 12 月 27 日
編集済み: Jan 2012 年 12 月 27 日
You can simply use the index vectors to resort rows and columns:
u = [11 12 13 14; ...
21 22 23 24; ...
31 32 33 34; ...
41 42 43 44];
B1 = [1 4 3 2];
B2 = [3 2 1 4];
u = u(B1, B2);
>> u =
13 12 11 14
43 42 41 44
33 32 31 34
23 22 21 24
  1 件のコメント
ss
ss 2012 年 12 月 29 日
and what should we do if size of matrix is bigger than B1 and B2, for instance (u)6*6 and B1 and B2 are 4*4? thanks

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 27 日
編集済み: Azzi Abdelmalek 2012 年 12 月 27 日
u=magic(4) % Example of u
n=size(u,1)
B1=randperm(n) % Example of B1
B2=randperm(n)' % Example of B2
u=u(:,B1)
u=u(B2,:)
  1 件のコメント
Jan
Jan 2012 年 12 月 27 日
You can omit the transposition for B2.

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

カテゴリ

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