简体   繁体   中英

matlab replace value in matrix

I am trying to replace the first and second column of an edgelist matrix (edgenumber x 3) by specific node numbers as such:

5   1   1
1   38  1
2   1   1
28  17  1
18  1   1
25  1   1

that the node numbers (connection from node 5 to node 1) are replaced by the corresponding values from a vector. The edgelist is generated from an unweighted 40x40 adjacency matrix. The vector degree_list of size 40x1 contains the "real"node numbers of this edgelist which i want to add to a larger 321x321 adjacency matrix. (if there's an easier way to do that then by concatenating the edgelists, that would also be great).

degree_list=[183,150,151,39,184,185,152,...];

So of the above edgelist I would like to replace all 1s in coll 1 and 2 by 183, all 2s by 150, etc.

Then I need to keep this new edgelist which I the add to the larger edgelist, transform it back to an adjacency matrix and have my new correct bigger adjM.

I have tried to find a solution here and on other websites but not been successful. Thank you very much for your help, Chris

Code

a1 = [
    5   1   1
    1   38  1
    2   1   1
    28  17  1
    18  1   1
    25  1   1]
degree_list=[183,150,151,39,184,185,152];
col12 = a1(:,[1 2])

col12_uniq =  unique(col12)
degree_list = [degree_list numel(degree_list)+1:max(col12_uniq)]

uniq_dim3 = bsxfun(@eq,col12,permute(repmat(col12_uniq,1,2),[3 2 1]))
match_dim3 = bsxfun(@times,uniq_dim3,permute(degree_list(col12_uniq),[3 1 2]))
a1_out = [sum(match_dim3,3) a1(:,3)]

Output

a1 =

     5     1     1
     1    38     1
     2     1     1
    28    17     1
    18     1     1
    25     1     1


a1_out =

   184   183     1
   183    38     1
   150   183     1
    28    17     1
    18   183     1
    25   183     1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM