Suppose your rows are stored in a matrix A
, and the column set widths are stored in len
(in your case that would be len = [4, 3, 3]
). First we should represent this data properly in a cell array:
X = mat2cell(A, ones(size(A, 1), 1), len);
Then we find all possible combinations of columns in such a cell array (without repetition):
cols = perms(1:numel(len));
Now, for given two rows from X
with indices r1
and r2
, we check if one is a permutation of the other (i.e reordered "mental" columns):
any(arrayfun(@(n)isequal(X(r1, :), X(r2, cols(n, :))), 1:size(cols, 1)))
Following this, we can now find all possible pairs of rows (without repetition), and for each pair of rows check if they are a 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 is as easy as pie:
A(remove_idx, :) = [];
Example
Let's take the following data as 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 the 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
A =
1 2 3 4 5 6 7 8 9 10
1 2 3 4 8 9 10 5 6 7