I'm trying to write a user-defined function which should determine whether there are any duplicate values in an array.

1 次查看(过去 30 天)
I'm trying to write a user-defined function. This function should determine if an array contains any values which occur more than once, and change the variable bmatch to 1 for true or 0 for false.
At the moment, I'm just trying to work out the algorithm of the function; the script will sometimes work and sometimes not, e.g., when I create an array [4 5 3 4 6 7 8] it won't detect the duplicate, but when the array is [4 5 3 4] it does detect the duplicate.
%data = [4 5 3 4 6 7 8];
for b = 1:length(data)
if sum(data(b) == data) > 1
bmatch = 1;
else
bmatch = 0;
end
end
Please let me know what I'm doing wrong, and if you have a solution to the issue.
  1 个评论
VBBV
VBBV 2023-4-22
编辑:VBBV 2023-4-25
May be you need this change in your code
data = [4 5 3 4 6 7 8 10 2 10 2 20 21 7];
for b = 1:length(data)
% this change
if sum(data(b)-data == 0) > 1
% uncomment the below line to show if there is a duplicate value
bmatch = 1;
fprintf('bmatch = %d Num %d\n',bmatch,data(b))
else
bmatch = 0;
end
end
bmatch = 1 Num 4 bmatch = 1 Num 4 bmatch = 1 Num 7 bmatch = 1 Num 10 bmatch = 1 Num 2 bmatch = 1 Num 10 bmatch = 1 Num 2 bmatch = 1 Num 7

请先登录,再进行评论。

采纳的回答

Sai Kiran
Sai Kiran 2023-4-25
Hi,
As per my understanding you want a user defined function which returns 1 if any value in the array gets repeated or else 0.
Your code detects the duplicate values but the variable 'bmatch' will only gives the case for the last value in the array. Adding a 'break' keyword whenever you detect a duplicate value ensures you to go out of the loop and give the correct answer.
Here is the modified code for the same.
for b = 1:length(data)
if sum(data(b) == data) > 1
bmatch = 1;
break;
else
bmatch = 0;
end
end
I hope it helps!
Thanks.
  3 个评论
Stephen23
Stephen23 2023-4-25
Note that you do not need to loop over all elements, nor is it required to compare against all elements on every iteration. If speed is important then consider other approaches e.g.:
checkdata1([4,5,3,4,6,7,8])
ans = logical
1
checkdata1([4,5,3,4])
ans = logical
1
checkdata2([4,5,3,4])
ans = logical
1
V = 1:1e5;
tic
checkdata0(V)
ans = logical
0
toc
Elapsed time is 8.944594 seconds.
tic
checkdata1(V)
ans = logical
0
toc
Elapsed time is 6.015339 seconds.
tic
checkdata2(V)
ans = logical
0
toc
Elapsed time is 0.003918 seconds.
function bmatch = checkdata0(data) % your function
bmatch = false;
for b = 1:length(data)
if sum(data(b)-data == 0) > 1
bmatch = true;
break
end
end
end
function bmatch = checkdata1(data) % only check remaining elements
bmatch = false;
for k = 2:numel(data)
if nnz(data(k-1)==data(k:end))
bmatch = true;
break
end
end
end
function bmatch = checkdata2(data)
bmatch = ~all(diff(sort(data)));
end
VBBV
VBBV 2023-4-25
编辑:VBBV 2023-4-25
@Jonah @Sai Kiran, One problem with using break inside a loop, is it exits the loop once a duplicate value of a number is found in vector and doesn't check if there are more than one duplicate numbers present in vector.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by