Hello.......i have a matrix.......what i wonaa do is count the repeated entries in column 2 first then make arrangement =(number of repeated termes)! ..for example in given matrix i have two repeated terms so first arrangmnt..1 2 3 the second 2 1 3
1 view (last 30 days)
Show older comments
inzamam shoukat
on 5 Dec 2018
Commented: inzamam shoukat
on 8 Dec 2018
a=[1 4;2 4;3 2]
required answer is
repeated terms=2
Arrangments are
1 4
2 4
3 2
&
2 4
1 4
3 2
2 Comments
Accepted Answer
Stephen23
on 5 Dec 2018
Edited: Stephen23
on 5 Dec 2018
Interesting problem. I used a slightly more complex example, with two values duplicated (4 twice, 9 thrice), for which we would expect twelve possible permutations:
>> a = [1,4;2,9;3,2;4,9;5,4;6,9]
a =
1 4
2 9
3 2
4 9
5 4
6 9
And the code:
[N,B] = histc(a(:,2),unique(a(:,2))); % count instances of values in 2nd column
X = find(N>1); % find duplicate values
foo = @(x)perms(find(B==x)); % row permutations for each duplicate value
C = arrayfun(foo,X,'uni',0); % "
S = cellfun('size',C,1); % count row permutations
L = arrayfun(@(r)1:r,S,'uni',0); % row indices for each permutation matrix
[L{:}] = ndgrid(L{:}); % combine row indices
R = cellfun(@(v)v(:),L,'uni',0); % "
baz = @(m,r)m(r,:); % use row indices to select all permutations
M = cellfun(baz,C,R,'uni',0); % "
V = 1:size(a,1); % original row indices of input matrix
Z = cell(1,prod(S)); % preallocate output cell array
for ii = 1:prod(S) % for each possible permutation...
for jj = 1:numel(X) % for each duplicate value...
V(B==X(jj)) = M{jj}(ii,:); % get row permutation indices
end %
Z{ii} = a(V,:); % use indices to select from input matrix
end
And checking the output:
>> numel(Z) % should be 3!*2!
ans = 12
>> Z{:}
ans =
1 4
2 9
3 2
4 9
5 4
6 9
ans =
5 4
2 9
3 2
4 9
1 4
6 9
ans =
1 4
4 9
3 2
2 9
5 4
6 9
ans =
5 4
4 9
3 2
2 9
1 4
6 9
ans =
1 4
2 9
3 2
6 9
5 4
4 9
ans =
5 4
2 9
3 2
6 9
1 4
4 9
ans =
1 4
4 9
3 2
6 9
5 4
2 9
ans =
5 4
4 9
3 2
6 9
1 4
2 9
ans =
1 4
6 9
3 2
2 9
5 4
4 9
ans =
5 4
6 9
3 2
2 9
1 4
4 9
ans =
1 4
6 9
3 2
4 9
5 4
2 9
ans =
5 4
6 9
3 2
4 9
1 4
2 9
Note that the number of permutations is (# of 1st duplicate value)! * (# of 2nd duplicate value)! * (# of 3rd duplicate value)! * ... , which will clearly explode very quickly into something totally intractable....
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!