Get rid of identical permutations in a matrix, Matlab -
i generated x-by-10 array of numbers matlab. array 'mentally' divided columns sets of 4, 3 , 3. 2 rows if array given below
[1 2 3 4 ; 5 6 7 ; 8 9 10]
[1 2 3 4 ; 8 9 10 ; 5 6 7]
the semi-colons mental divisions. need process array further, 'mental column' permutations give same information. second row permutation of second , third 'mental row' of first one.
is there simple way can rid of permutations built in functions of matlab ? sort of unique recognizes permutations.
suppose rows stored in matrix a
, , column set widths stored in len
(in case len = [4, 3, 3]
). first should represent data in cell array:
x = mat2cell(a, ones(size(a, 1), 1), len);
then find possible combinations of columns in such cell array (without repetition):
cols = perms(1:numel(len));
now, given 2 rows x
indices r1
, r2
, check if 1 permutation of other (i.e reordered "mental" columns):
any(arrayfun(@(n)isequal(x(r1, :), x(r2, cols(n, :))), 1:size(cols, 1)))
following this, can find possible pairs of rows (without repetition), , each pair of rows check if permutation of each other:
rows = nchoosek(1:size(a, 1), 2); n = size(cols, 1); isperm = @(ii, jj)any(arrayfun(@(n)isequal(x(ii, :), x(jj, cols(n, :))), 1:n)); remove_idx = arrayfun(isperm, rows(:, 1), rows(:, 2));
and removing them easy pie:
a(remove_idx, :) = [];
example
let's take following data input:
a = [1:10; 11:20; 1:4 8:10 5:7]; len = [4 3 3];
that is:
a = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 8 9 10 5 6 7 len = 4 3 3
and run following code:
x = mat2cell(a, ones(size(a, 1), 1), len); cols = perms(1:numel(len)) rows = nchoosek(1:size(a, 1), 2) n = size(cols, 1) isperm = @(ii, jj)any(arrayfun(@(n)isequal(x(ii, :), x(jj, cols(n, :))), 1:n)); remove_idx = arrayfun(isperm, rows(:, 1), rows(:, 2)); a(remove_idx, :) = [];
the result is:
remove_idx = 0 1 0 = 1 2 3 4 5 6 7 8 9 10 1 2 3 4 8 9 10 5 6 7
Comments
Post a Comment