In: Computer Science
The following code must be written using matlab
How to get the elements that are different in two set without using the setdiff function in matlbab?
Here is the code to find the elements that are different in two
sets by using for loops:
%define two arrays
a1 = [1,5,2,7,8];
a2 = [5,4,9,1,3,11,7];
diff = []; %initialize an array for diff
%go over the first array
for i=1:length(a1)
%check if element present in second array
if ~ismember(a1(i), a2)
%add to diff is not present
diff = [diff a1(i)];
end
end
%now go over the second array
for i=1:length(a2)
%check if element present in first array
if ~ismember(a2(i), a1)
%add to diff is not present
diff = [diff a2(i)];
end
end
%print the answer
diff
Here is a screenshot of the code along with the output of the
code:
Comment in case of any doubts.