How to print duplicate (repeated) value from an array?

Dear experts,
I have to print the duplicate (repeated) value from the following array. Please help me.
104.96
81.01
-35.21
-150.76
145.22
104.96
20.62
-90.79

2 个评论

Have you tried my edited answer?
Yes sir. It works with data I had given before. But not for the data generated from the codes attached with the previous comment.

请先登录,再进行评论。

 采纳的回答

More simpler way:
[C,~,~]=unique(v);
val=sum(v)-sum(C)
Edit after Image Analyst's warning:(works for any situation)
[C,~,idx]=unique(v,'stable');
n=accumarray(idx(:),1);
vals=C(find(n~=1))

3 个评论

Clever, and simpler but doesn't work if the number is repeated more than twice or there are two numbers that are repeated, like mine does. Just try it with
v = [...
104.96
81.01
-35.21
-150.76
145.22
104.96
104.96
20.62
-90.79]
[C,~,~]=unique(v);
val=sum(v)-sum(C)
You get val = 209.92
Or with
v = [...
104.96
81.01
-35.21
-150.76
145.22
104.96
-35.21
20.62
-90.79]
You get val = 69.75.
However it does work for the one array that he posted and that's all that was asked for - it didn't ask to generalize it. My solution works in both cases and also gives the index where the repeat(s) occurred.
Dear Image Analyst (Expert), Thanks for the help.
The code given by you works well in separate window. But, not along with code attached with this comment.
This is probably due to approximation in the values.
Will you please help me to resolve this issue?
Thanks in advance.
%%%%%%%%%%%%%%%%Code%%%%%%%%%%%%%%%%%%%% Eq1(x, y) = - (7542936551605719*x^4*y^4)/1125899906842624 + (10696071671072981*x^4*y^2)/1125899906842624 - (2027189923366279*x^4)/1125899906842624 + (344734164264965*x^3*y^3)/17592186044416 + (344734164264965*x^3*y)/17592186044416 + (11483540892015981*x^2*y^4)/562949953421312 + (37717533301837501*x^2*y^2)/562949953421312 + (5967794263776541*x^2)/562949953421312 + (344734164264965*x*y^3)/17592186044416 + (344734164264965*x*y)/17592186044416 - (5518597172048345*y^4)/1125899906842624 - (7318236082770031*y^2)/1125899906842624 - 22065837056766665/1125899906842624; Eq2(x, y) = (7824422850630965*x^4*y^3)/1125899906842624 - (2308676222391525*x^4*y)/1125899906842624 + (10019323566700193*x^3*y^4)/1125899906842624 + (4503576938460753*x^3*y^2)/562949953421312 - (1012169689778687*x^3)/1125899906842624 + (2308676222391525*x^2*y^3)/562949953421312 - (7824422850630965*x^2*y)/562949953421312 - (7994984187142819*x*y^4)/1125899906842624 - (13510730815382259*x*y^2)/562949953421312 - (19026477443621699*x)/1125899906842624 - (3207070405847915*y^3)/1125899906842624 - (13340169478870405*y)/1125899906842624; Eq1(x,y)=simplify((subs(Eq1(x,y),{a1,a2,a3},[1.41421 1.41421 0.8660254]))); Eq2(x,y)=simplify((subs(Eq2(x,y),{a1,a2,a3},[1.41421 1.41421 0.8660254]))); E1=Eq1(x,Y(1,1)); [X1]=double(solve(E1));
E2=Eq2(x,Y(1,1)); [X2]=double(solve(E2)); [X]=[X1;X2]; format bank % bank displays output in its shortest form, like fprintf statement for i = X(:,1); ver_1=double(Eq1(i,Y(1,1))); ver_2=double(Eq2(i,Y(1,1))); [ver] = [ver_1 ver_2]; end for i= X(:,1) [v]=atand(X)*2; end [C,~,idx]=unique(v,'stable'); n=accumarray(idx(:),1); vals=C(find(n~=1))
Hareesha, it looks like you accepted Birdman's answer, and that doesn't require the stats toolbox, so go with that.
Also, you might want to read this link

请先登录,再进行评论。

更多回答(2 个)

Here is one way:
v = [...
104.96
81.01
-35.21
-150.76
145.22
104.96
20.62
-90.79]
distances = pdist2(v, v) % In Stats toolbox
% Find where distances = 0
[rows, columns] = find(distances == 0)
for k = 1 : length(rows)
if rows(k) ~= columns(k) % Ignore diagonals
fprintf('Element at row %d (%f) is a repeat.\n', rows(k), v(rows(k)));
end
end
It shows:
Element at row 6 is a repeat.
Element at row 1 is a repeat.

1 个评论

Sir, This code didnt work, as i dont have statistics toolbox. Thanks for your time!!

请先登录,再进行评论。

hi, please help me to find duplication in sequence of image and send me a code .

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by