Question & Answer: Write a MATLAB algorithm and scripts to solve the following question(suggested variable names in brackets):…..

Write a MATLAB algorithm and scripts to solve the following question(suggested variable names in brackets):

Using for loops, write a script which counts for a vector v (ask user input by asking how many numbers and then asking for each number one input at a time) how many times each element exists in the vector. Display the result as a well formatted table also using a for loop.

Expert Answer

Please find below the solution for above problem:

count.m

%asking from user to no of elements
len=input(“Enter the No of elements in vector : “);
%asking from user to enter each element
for k = 1:len
x(k)=input(‘Enter no : ‘);
end
%copied to temp
temp=x;
%taking y vector for storing the count
y=zeros(size(x));
%using for loop counting the no og elements
for i = 1:length(x)
if(length(x)<=0)
break
end
y(i)=sum(x==x(1));
%after counting that variable removing from vector
x=x(x~=x(1));
end
%displaying the result in tabular format
disp(“Elements Occurence”);
%using for loop for displaying the elements
for j = 1:length(temp)
if(length(temp)<=0)
break
end
fprintf(‘ %d ‘,temp(1));
fprintf(‘ %dn’,y(j));
temp=temp(temp~=temp(1));
end
%end of script

OUTPUT

Still stressed from student homework?
Get quality assistance from academic writers!