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

 Akzeptierte Antwort

goran
goran am 19 Jan. 2014

0 Stimmen

i changed matrix to 4x4 and i sould received like solution duplicate above main diagonal nuber 4, i receive solution 3. why?

2 Kommentare

Mischa Kim
Mischa Kim am 19 Jan. 2014
Hello goran, could you please post follow-up questions as comments, not as answers? To your question: the first duplicate above the diagonal (searching from left to right, top to bottom) is the 3 at position (1,2).
goran
goran am 19 Jan. 2014
sorry. code is ok. can you explain your code? what is FLAG?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (5)

Mischa Kim
Mischa Kim am 19 Jan. 2014
Bearbeitet: Mischa Kim am 19 Jan. 2014

0 Stimmen

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 Kommentare

goran
goran am 19 Jan. 2014
can you explain me your code, i must print message 'no duplicate' if there're not duplicates?
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
goran am 19 Jan. 2014
again i puted image
first duplicate is 2. number 2 is found under main diagonal. if i need searching above main diagonal what i do?
Mischa Kim
Mischa Kim am 19 Jan. 2014
The algorithm only searches for duplicates above the diagonal. The 2 found is the one in the first row, second column.
goran
goran am 19 Jan. 2014
what's code would be if i search in range above main diagonal?this code does not work with real numbers
goran
goran am 19 Jan. 2014
when my matrix has elements like on pictures then no duplicates why?
i think that duplicate is 1.1
my solution for this problem is
if true
A=[1 3 2.2 130 5.4 60 8.9 8 45;2 58 4 8 6.5 69 78 23 1.1;7 8 4 69 65.3 6.7 89.3 102.3 45;3 4 3 1 8.9 1.1 59 4.5 65;
1 23 2.5 45 5.6 7.9 8.78 65 89; 12 897 67 0.5 102.3 7.8 9 11 567; 494 123 256 28.6 2.2 123 891 111 11.1;
45 12 1.2 2.6 58.4 36 45.7 12.1 78.6; 1.1 2.3 4.5 36 25 11.6 89.3 1.1 1]
%A=rand(9,9)
Agd=(triu(A, 1)); %selektuje elemente iznad glavne diagonale
Agdv=Agd(:); %kreira vektor
Agdv(Agdv==0)= []; %brise nule iz vektora
Aj=unique(Agd); %selektuje jedinstvene vrednosti iz Agd
Aj(Aj==0)=[]; %brise nule iz jedinstvenih vrednosti
if (length(Aj)~=length(Agdv)) %proverava da li broj jedinstvenih vrednosti razlicit u odnosu na Agdv, ako jesu onda postoje duplikati, ako ne stampa nema istih elemenata
[~,~, IAgdv] = unique(Agdv, 'stable'); %odredjuju se indeksi jedinstvenih vrednosti u nepromenjenom redosledu u Agdv
idx = find(diff(IAgdv)-1, 1)+1; %nalazi indeks koji se dva puta ponavlja
el=Agdv(idx); %dodeljuje vrednost tog indeksa promenljivoj el
fprintf('Prvi isti element koji se pojavio je: %.4f \n',el) % stampa vednost promenljive el
else
disp('Nema istih elemenata')
end
end

Melden Sie sich an, um zu kommentieren.

goran
goran am 19 Jan. 2014

0 Stimmen

the searching need to work just on elements above main diagonal. your code search entire matrix

1 Kommentar

Mischa Kim
Mischa Kim am 19 Jan. 2014
Bearbeitet: Mischa Kim am 19 Jan. 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.

Melden Sie sich an, um zu kommentieren.

goran
goran am 19 Jan. 2014

0 Stimmen

also, i need that duplicate values is printed, not how much duplicate values have?

1 Kommentar

Mischa Kim
Mischa Kim am 19 Jan. 2014
It is. See the display command. Simply run the code above, change the matrix and verify.

Melden Sie sich an, um zu kommentieren.

Andrei Bobrov
Andrei Bobrov am 19 Jan. 2014
Bearbeitet: Andrei Bobrov am 20 Jan. 2014

0 Stimmen

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 Kommentare

goran
goran am 19 Jan. 2014
can you explain me this code, if in matrix there're not duplicates, i need print message 'no duplicates'
goran
goran am 20 Jan. 2014
i need print message if no duplicates. what i do?
Andrei Bobrov
Andrei Bobrov am 20 Jan. 2014
Bearbeitet: Andrei Bobrov am 20 Jan. 2014
use file test1.m:
goran
goran am 20 Jan. 2014
thank's sorry can you explain your code?

Melden Sie sich an, um zu kommentieren.

Harry Commin
Harry Commin am 9 Feb. 2014

0 Stimmen

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.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Jan. 2014

Beantwortet:

am 9 Feb. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by