Question

In: Computer Science

The following code must be written using matlab How to get the elements that are different...

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?

Solutions

Expert Solution

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.


Related Solutions

The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
These instructions are written with the assumption that code will be done in matlab. You might...
These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max. 2. Numerical Integration (Quadrature). Write FOUR of your own numerical integration routines. One should use left-end, right-end, or mid-points, another should use the trapezoid method, another should use Simpson’s method, and the fourth should use either Guassian Quadrature or Romberg’s method. Use your four...
These instructions are written with the assumption that code will be done in matlab. You might...
These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max. 3. Root finding; Bisection, Secant, Newton. a) Write your own version of the bisection method to solve e 2x − 10x = 0. Assume the root is in the interval [0, 1]. Try to use the matlab command ”fzero” to accomplish the same thing....
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
get any written contract (from any source) and then show the following elements of this contract:...
get any written contract (from any source) and then show the following elements of this contract: 1. The names of the contracting parties. 2. The date and the place of the contract. 3. The subject of the contract. 4. The consideration. 5. The main obligations of the contracting parties. 6. Any other specific terms and conditions.
Give me a MATLAB code which integrates any function using Romberg integration The code must work...
Give me a MATLAB code which integrates any function using Romberg integration The code must work fine or else I will down vote the answer badly Dont post if you dont know
VHDL code for a 4 to 2 priority encoder. The structural behavior must be written using...
VHDL code for a 4 to 2 priority encoder. The structural behavior must be written using gates.
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT