开发者

Construct matrix according to the arrangement in another matrix

开发者 https://www.devze.com 2023-02-13 02:36 出处:网络
I have a matrix \'eff_tot\' with dimension (m x n) which I want to rearrange according to a matrix called \'matches\' (e.g. [n2 n3; n4 n5]) and put all the collumns not specified in \'matches\' at the

I have a matrix 'eff_tot' with dimension (m x n) which I want to rearrange according to a matrix called 'matches' (e.g. [n2 n3; n4 n5]) and put all the collumns not specified in 'matches' at the end.

That is, I want to have [eff_tot(:,n2) eff_tot(:,n3) ; eff_tot(:,n4) eff_tot(:,n5) ; eff_tot(:,n1)].

That's all folk开发者_StackOverflow中文版s!

Taking the example in the first answer, what I would like to have is:

eff_tot =

81    15    45    15    24
44    86    11    14    42
92    63    97    87     5
19    36     1    58    91
27    52    78    55    95
82    41     0     0     0
87     8     0     0     0
 9    24     0     0     0
40    13     0     0     0
26    19     0     0     0

Regards.


Create a vector listing the indices of all the columns in eff_tot and then use SETDIFF to determine which columns do not occur in [n2 n3 n4 n5]. These columns are the unmatched ones. Now concatenate the matched and unmatched column indices to create your column-reordered eff_tot matrix.

>> eff_tot = randi(100, 5, 7)

eff_tot =

    45    82    81    15    15    41    24
    11    87    44    14    86     8    42
    97     9    92    87    63    24     5
     1    40    19    58    36    13    91
    78    26    27    55    52    19    95

>> n2 = 3; n3 = 5; n4 = 2; n5 = 6;
>> missingColumn = setdiff(1:size(eff_tot, 2), [n2 n3 n4 n5])

missingColumn =

     1     4     7

>> eff_tot = [eff_tot(:,n2) eff_tot(:,n3) eff_tot(:,missingIndex); eff_tot(:,n4) eff_tot(:,n5) zeros(size(eff_tot, 1), length(missingIndex))];

eff_tot =

    81    15    45    15    24
    44    86    11    14    42
    92    63    97    87     5
    19    36     1    58    91
    27    52    78    55    95
    82    41     0     0     0
    87     8     0     0     0
     9    24     0     0     0
    40    13     0     0     0
    26    19     0     0     0
0

精彩评论

暂无评论...
验证码 换一张
取 消