matrix 9x9 with duplicate values
Mostrar comentarios más antiguos
i have matrix C9x9 with duplicates. i must find duplicate above main diagonale. when i find first duplicate the searching stop and print this duplicate? thanks
Respuesta aceptada
Más respuestas (5)
Mischa Kim
el 19 de En. de 2014
Editada: Mischa Kim
el 19 de En. de 2014
This should do. For a 3x3 matrix, as an example:
A = [1 3 2; 4 5 1; 3 1 1]
A_unique = unique(tril(A, -1));
FLAG = false;
for ii = 1:length(A(1,:))
for jj = ii+1:length(A(1,:))
if (length(A_unique) == length(unique([A_unique; A(ii,jj)])))
display(A(ii,jj))
FLAG = true;
break;
end
end
if FLAG
break;
end
end
7 comentarios
goran
el 19 de En. de 2014
Mischa Kim
el 19 de En. de 2014
First, find the unique values of A and write them into a vector. Next enter the nested for loops where you are searching for duplicates: add the current matrix entry to the vector of unique values and test if that changes the vector. If it does not, this means that the entry must be a duplicate. In this case set the FLAG variable to true, which is necessary to end ( break ) the nested loop.
Add
if ~FLAG
display('No duplicates')
end
goran
el 19 de En. de 2014
Mischa Kim
el 19 de En. de 2014
The algorithm only searches for duplicates above the diagonal. The 2 found is the one in the first row, second column.
goran
el 19 de En. de 2014
goran
el 19 de En. de 2014
goran
el 23 de En. de 2014
goran
el 19 de En. de 2014
0 votos
1 comentario
Mischa Kim
el 19 de En. de 2014
Editada: Mischa Kim
el 19 de En. de 2014
Hello goran, the algorithm only searches above the diagonal. That's because of the indexing of the for loop ( jj = ii+1! ):
for ii = 1:length(A(1,:))
for jj = ii+1:length(A(1,:))
You can verify by simply printing the indices of the matrix elements.
goran
el 19 de En. de 2014
0 votos
1 comentario
Mischa Kim
el 19 de En. de 2014
It is. See the display command. Simply run the code above, change the matrix and verify.
Andrei Bobrov
el 19 de En. de 2014
Editada: Andrei Bobrov
el 20 de En. de 2014
function test1
A = randi(15,3)
B=A;
B(tril(B)>0)=nan;
C=B(~isnan(B));
[a,b] = unique(C,'first');
[~,ii] = sort(b);
c = histc(C,a);
out0 = [a(ii),c(ii)];
out = out0(find(out0(:,2)>1,1,'first'),1);
if isempty(out), disp('no duplicates'); end
end
4 comentarios
goran
el 19 de En. de 2014
goran
el 20 de En. de 2014
Andrei Bobrov
el 20 de En. de 2014
Editada: Andrei Bobrov
el 20 de En. de 2014
use file test1.m:

goran
el 20 de En. de 2014
Harry Commin
el 9 de Feb. de 2014
To extract only the upper triangular numbers into a column vector, you could use:
Aupper = A(triu(ones(size(A)))==1);
I think it is easier to find all duplicates than just the 'first' one. (How do we even define "first"?). However, assuming we want to progress through A column-wise, we could use:
B = unique(Aupper,'stable');
first_duplicate = Aupper(find(Aupper(1:length(B)) ~= B, 1))
The first line finds unique values in the order they appear. The second line finds the first place where the input vector and the 'uniques' are different (i.e. the first duplicate) and prints out that value.
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


