Hello, please show the code for the following problems using MATLAB. Please incl
ID: 3793288 • Letter: H
Question
Hello, please show the code for the following problems using MATLAB. Please include well detailed comments so that I can understand all functions and processes. Thanks in advance.
The purpose of this assignment is to use MATLAB for implementing classes. 1. Sets (70 points) Implement an abstract data type (class) "SET OF INTEGERS" with operation UNION" and "INTERSECTION". The name of your class should be called setIntegers. The set constructor should accept a vector of integers or implement a method used to add integers and update the set of integers property of your class. 2. Test Case (20 points Implement a script to test the Set class setIntegers. Look at the hint below. Also implement a method called integerSet in the setIntegers class that prints out the sets of integers. 3. Comments and reports (10 points) 4. Bonus (10 points) Write a function, called in your Test Case script that reads from a txt file containing two columns of integers. Find the union and intersection of the two column of integers. Hints: The intersection of two sets are those elements that belong to both sets. The union of two sets are all the distinct elements from both sets. a set Integers (C1,2,3,5]) b set Integers (C2,6, 7]) a union (b) [1,2,3,5,6,7] a. intersection (b) [2] a set Integers (C1,3,5, 71) b setIntegers([2,6,8]) a .union (b) [1,2,3,5,6,7,81 a. intersection (b) CJ a set [1,3,5,7,1]) Integers a.integerset [1,3,5,7]Explanation / Answer
Matlab class
classdef setIntegers % The class setIntegers
properties
values % variable to store the array of each object
end
methods
function obj = setIntegers(val) % Function to assign the numbers to the variable values
if nargin > 0 % checking the number of argumant
obj.values = val; % in the case of argument assign the val to values of that onject
end
end
function integerSet(obj) % Function to print the values in the object
N = length(obj.values); % number of elements in the array values
v = '['; % Creating a character string to display the array
for k = 1:N-1 % adding each element to v with coma
v = [v num2str(obj.values(k)) ','];
end
if N~=0 % adding the last element.no need of coma
v = [v num2str(obj.values(N))];
end
v = [v ']'];
disp(v); % displaying the values in the prescribed format
end
function union(obj1,obj2) % function to compute the union of two objects
% union in the following command is a matlab function
% creating an object with values are the union of values in the
% other two objects
if nargin == 2 % if the number of argument is two
c = setIntegers(union(obj1.values,obj2.values));
else % if one argumant
c = setIntegers(union(obj1.values,obj1.values));
end
% calling integerSet() function to print the values in object c
c.integerSet();
end
function intersection(obj1,obj2)
% intersect in the following command is a matlab function
% creating an object c with values are the in the intersection
% of values in the other two objects
if nargin == 2 % if the number of argument is two
c = setIntegers(intersect(obj1.values,obj2.values));
else % if one argumant
c = setIntegers(intersect(obj1.values,obj1.values));
end
% calling integerSet() function to print the values in object c
c.integerSet();
end
end
end
Matlab function
function readDataForTest()
fileID = fopen('nums.txt','r'); % opening the file contained data
formatSpec = '%f %f'; % format of reading the data
sizeA = [2 Inf];% size of A
A = fscanf(fileID,formatSpec,sizeA);% Reading the data from the file
fclose(fileID);% Closing the file after reading
a = setIntegers(A(1,:)); % Creating an object of setIntegers with values
% are the data in the first column
b = setIntegers(A(2,:)); % Creating an object of setIntegers with values
% are the data in the second column
disp('Union: ');
a.union(b); % finding the union of the vectors
disp('Intersection: ');
a.intersection(b); % finding the intersection of the vectors
end
nums.txt
1 3
2 2
3 5
5 9
6 8
Testing the code
>> a = setIntegers([1,2,3,5]);
>> b = setIntegers([2,6,7]);
>> a.union(b)
[1,2,3,5,6,7]
>> a.intersection(b)
[2]
>>
>> a = setIntegers([1,3,5,7]);
>> b = setIntegers([2,6,8]);
>> a.union(b)
[1,2,3,5,6,7,8]
>> a.intersection(b)
[]
>> a = setIntegers([1,3,5,7,1]);
>> a.intersection()
[1,3,5,7]
>>
>> readDataForTest()
Union:
[1,2,3,5,6,8,9]
Intersection:
[2,3,5]
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.