Search parallel rows of a structures for common items in matlab

前端 未结 1 1878
清歌不尽
清歌不尽 2021-01-24 06:58

I\'ve stored (row,col,val) information of

 key1:         (1,1) (1,2) (1,3) (4,2) (3,4)  
 attribute1:    2      3    4      2     5  

as follo

相关标签:
1条回答
  • 2021-01-24 07:19

    first use sort to achieve row-col order insensitivity, next use unique to handle row-col duplicates within each structure, and finally use ismember (with 'rows') to find common keys between the structures. note that I added an inner duplication to each structure to show the second stage effect:

    % struct1
    row1 = [1  1  1  2  4  3];
    col1 = [1  2  3  1  2  4];
    att1 = [2  3  4  6  2  5];
    % struct2
    row2 = [2  2  1  3  3];
    col2 = [1  2  3  1  4];
    att2 = [1  0  1  1  5];
    % sort in 2nd dimension to get row-column indexes insensitive for order
    idx1 = sort([row1(:) col1(:)],2);
    idx2 = sort([row2(:) col2(:)],2);
    % search for duplicates inside each struct
    [idx1,~,bins1] = unique(idx1,'rows','stable');
    att1 = accumarray(bins1,att1);
    [idx2,~,bins2] = unique(idx2,'rows','stable');
    att2 = accumarray(bins2,att2);
    % search common entries
    common1 = ismember(idx1,idx2,'rows');
    row = idx1(common1,1);
    col = idx1(common1,2);
    common2 = ismember(idx2,[row col],'rows');
    % add common values
    att = att1(common1) + att2(common2);
    Result.row = row';
    Result.col = col';
    Result.attribute = att';
    disp(Result)
    

    and you get:

    Result = 
              row: [1 1 3]
              col: [2 3 4]
        attribute: [10 6 10]
    
    0 讨论(0)
提交回复
热议问题