Matlab Program Help! Please provide the code to the question in the picture belo
ID: 2080019 • Letter: M
Question
Matlab Program Help! Please provide the code to the question in the picture below. Thanks
Write a program that starts with two given arrays u and v with arbitrary sizes such as below u = [1, 2, 3, 4, 3, 4, 3, 2, 1, 2, 4, 6], v = [4, 7, 18, 6]. In Part (a), your program should ask the user for an integer n. Then it should count how many times the integer n appears in the array u. If n does not appear in u, your program should add n as a new element to the end of the array u. Otherwise, your program should print The input appears [x] times". In Part (b), the program should find the common elements between u and v and store them in the array w. In the above example, w = [4, 6].Explanation / Answer
Ans)
Matlab code
================
n=input('Enter a positive integer:');
u=[1 2 3 4 3 4 3 2 1 2 4 6];
v=[4 7 18 6];
count=0;
for i=1:length(u)
if n==u(i)
count=count+1; %counts no of times 'n' appears
end
end
if count==0 %if 'n' doen't appear in u
u=[u n]; %add 'n' to the end of u
u
else
fprintf('The input appears %d times ',count);
end
%part-b
w=intersect(u,v);
w
=======================
sample results obtained when run
====
>> common_terms
Enter a positive integer:4
The input appears 3 times
w =
4 6
>>
==========
result 2 when there no 'n' terms
====
>> common_terms
Enter a positive integer:5
u =
Columns 1 through 12
1 2 3 4 3 4 3 2 1 2 4 6
Column 13
5
w =
4 6
>>
============