Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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
madhan ravi
madhan ravi el 5 de Dic. de 2018
what do you mean 2 and 4 are repeated twice? what‘s the rule?
inzamam shoukat
inzamam shoukat el 5 de Dic. de 2018
Editada: inzamam shoukat el 5 de Dic. de 2018
Actually i have to scan only column 2 to chk repeated terms....and then then arrange the matrix as given i can do it by sortroes cammand but it can give me only 1 answer i need factorial times arrangments...... column 1 enteries are just serial number there is nothing to do with it imagine it was not even there
repeated terms=2
Arrangments are
1 4
2 4
3 2
&
2 4
1 4
3 2

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
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)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by