Tallying the number of 'variable = true' which occurs over a number of permutations of a function.

1 回表示 (過去 30 日間)
Jonah
Jonah 2023 年 4 月 25 日
回答済み: Zuber 2023 年 5 月 12 日
Briefly: I'm trying to write a code which calls a function to test if an array (of random size with randomly assigned values) contains any duplicate values, and tallies depending on whether there was a duplicate (1) or not (0). I want it so the user can enter the number of permutations of this test, and have it so the tally will count with each permutation.
Here is what I've written:
Num_Permutations = input('enter a number between x and y');
d = 1:(Num_Permutations);
while InputInvalid == 1
groupsize = input('Please enter the size of the group you would like to test, between 2 and 365 people: ');
%This input determines the size of the array
if groupsize >= 2 && groupsize <= 365
InputInvalid = 0;
else
InputInvalid = 1;
disp('Error: Invalid Input. Please enter a group size between 2 and 365');
end
end
for d = 1:length(Num_Permutations)
while InputInvalid == 1
data = randi([1, 365], 1, groupsize);
%This assigns a random number between 1 and 365 to each value in the array
bmatch = checkdata(data);
%bmatch is the function I'm calling, when bmatch = 1, at least 1 duplicate has been found, when bmatch = 0, no duplicates have been found.
if bmatch == 1
Tally = Tally + 1;
%I'm trying to get it so that when the function finds a duplicate (bmatch = 1), the tally increases by 1.
else
Tally = Tally + 0;
end
end
end
%Here is the function I am calling:
function bmatch = checkdata(data)
bmatch = 0;
for b = 1:length(data)
if sum(data(b)-data == 0) > 1
bmatch = 1;
break
end
end
end
If you are savvy with The Lab, and you are feeling generous with your time, please give me a hand. It is much appreciated! Thank you.
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 4 月 25 日
You need to define the variable InputInvalid before using it as a parameter in the (1st) while loop.
The while loop inside the for loop will run infinitely as you have not set a break point. Though I don't understand what the point of that while loop is.
And there are faster methods to check for duplicate elements in an array, as @Stephen23 has pointed in your previous question.

サインインしてコメントする。

回答 (1 件)

Zuber
Zuber 2023 年 5 月 12 日
Hi,
I understand that you are facing an issue while counting the number of arrays containing duplicate elements in a user defined set. Please refer to the following steps to get the desired result:-
  • In the script, the variable ‘InputInvalid’ is not defined in the beginning of the code, thus, it will throw an error.
  • Further, it is required to initialize the ‘Tally’ variable.
  • I suggest inserting the following lines at the beginning of the code:
InputInvalid=1;
Tally=0;
  • Also, it seems that the second line defining array 'd' is redundant. So, you can consider removing it.
  • Additionally, within the for loop, a while loop is not required since it lacks a terminating condition, resulting in an infinite loop. So, kindly remove the following line of code inside the for loop:
while InputInvalid == 1
  • The function ‘checkdata’ will not generate desired results. This is because the expression ‘data(b)-data’ generates a vector by subtracting ‘data(b)’ from each element in the array ‘data’. However, when comparing this vector with 0, it will not produce the correct result, since it is not necessary that all the elements of array are identical even if it contains duplicate elements.
  • The function 'checkdata' can be modified using ‘unique’ function as follows:-
function bmatch = checkdata(data)
bmatch = 0;
if length(data) ~= length(unique(data)) %check if the number of unique elements in array is not equal to the length of array
bmatch = 1;
end
end
For more information on ‘unique’ function, please refer to the following documentation: https://www.mathworks.com/help/matlab/ref/double.unique.html
I have also attached the modified script for your reference. I hope it resolves your query.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by