0
votes

In Matlab...

Here is a sample from a larger matrix:

 5    11     9    12
 5    11    12     9
 5     9    11    12
 5     9    12    11
 5    12     9    11
 5    12    11     9
12    11     9     7
12    11     7     9
12     9    11     7
12     9     7    11
12     7     9    11
12     7    11     9
11    12     9     7
11    12     7     9
11     9    12     7
11     9     7    12
11     7     9    12
11     7    12     9
 9    11    12     7
 9    11     7    12
 9    12    11     7
 9    12     7    11
 9     7    12    11
 9     7    11    12
 7    11     9    12
 7    11    12     9
 7     9    11    12
 7     9    12    11
 7    12     9    11
 7    12    11     9

I'd like some code that will look through this matrix and find all of the vectors that transpose to each other, e.g.

7    12     9    11

and

6    11     8    10

and create a new matrix that contains only one version for each of the transposing vectors, in its lowest transposition available on the original matrix (i.e. if the original matrix contained:

7    12     9    11

and

6    11     8    10

Then I would only want to keep

6    11     8    10

(Similarly I'd like the list to keep any vectors for which there are no transpositions)

1
Could you elaborate on what do you mean by vectors that transpose to each other? - Divakar
Agreed. It's unclear what the implied relationship is between those two example vectors. - Oliver Charlesworth
Sorry - each figure in the second is one integer less than in the first one. Another vector that i would describe as transposed, would be: 9 14 11 13 - how should I have described this? - Sam Leak
Start with N=bsxfun(@minus,M(:,1),M) to get a matrix where the rows which you call transposed are equal, then use unique to identify the rows which are transposed to each other [~,~,index]=unique(N,'rows') - Daniel
Thank you - I'm struggling to make the... [~,~,index]=unique(N,'rows') ...stage work... could you make that a bit more dunce proof? All i've done currently is copy and paste that line in - should I have changed parts of it (apologies for general stupidity...) - Sam Leak

1 Answers

0
votes

Assuming A as the input array, here's one loopy approach -

out = [];
A_cut = A;
while size(A_cut,1)~=0
    grp_mask = all(diff(bsxfun(@minus,A_cut,A_cut(1,:)),[],2)==0,2)
    grpIDs = find(grp_mask)
    [~,min_idx] = min(A_cut(grpIDs,1))
    out_add = A_cut(grpIDs(min_idx),:)
    out = [out ; out_add];
    A_cut = A_cut(~grp_mask,:)
end

Sample input, output -

A =
     9     9     3     2
     5    10     7     9
     5    11     9    12
     6    11     8    10
    12    11     9     7
     7    12     9    11
     8     8     2     1
out =
     8     8     2     1 %// replaces (8,8,2,1) & (9,9,3,2)
     5    10     7     9 %// replaces (5,10,7,9),(6,11,8,10) & (7,12,9,11)
     5    11     9    12
    12    11     9     7