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 follows:
Structure A1:
key row1: 1 1 1 4 3
key col1: 1 2 3 2 4
attribute1: 2 3 4 2 5
Similarly, for structure A2
Structure A2:
key row2: 2 2 1 3
key col2: 1 2 3 4
attribute2: 1 0 1 5
Now, I'd like to be able to search for common entries in both row and column key items between structure A1 and A2 simultaneously. That is conceptually find [common_item, index]=intersect([row2,col2],[row1,col1]). I'd like the final result is insensitive to the order of row and col. So, in my example (1,2) key value is equal to (2,1) value. Then, the attribute value of common entries should be added together. The intended result is
Structure Result: //it recognizes (1,2) and(2,1) are the same.
key row: 2 1 3
key col: 1 3 4
attribute: 4 5 10
How should I proceed to search for common entries and do some operation ? ismember
function can search for common item just in one row and if there are multiple occurrence, it just count the first. In addition, as I said, I want it to be order insensitive in key values.
Thanks for any help.
first use
sort
to achieve row-col order insensitivity, next useunique
to handle row-col duplicates within each structure, and finally useismember
(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:and you get: