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 visualización (últimos 30 días)
Mostrar comentarios más antiguos
inzamam shoukat
el 5 de Dic. de 2018
Comentada: inzamam shoukat
el 8 de Dic. de 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 comentarios
Respuesta aceptada
Stephen23
el 5 de Dic. de 2018
Editada: Stephen23
el 5 de Dic. de 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....
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!