Write a matlab Function: [a, b] = index(v) which returns vectors a and b, given
ID: 3789087 • Letter: W
Question
Write a matlab Function:
[a, b] = index(v)
which returns vectors a and b, given a vector v of numeric values. Vector a must contain the unique values in the vector v, aand be must cntain the corresponding number of occurences of the unique values in v.
V contains values: v=[1 4 3 4 5 4 3]
and then; a=[1 4 3 5] b=[1 3 2 1]
You may assume a function find_index(v,c) is available which returns the index of the position of value c in vector v is c is present in v and -1 if c is not present. EXAMPLE: find_index([2 4 5], 4) will return 2, and find_index([1 4 3], 8) will return -1. Do not write the function find_index(v,c), and you ARE NOT permitted to use any oither Matlab inbuilt functions.
Explanation / Answer
function [a,b]=index(v)
x=1
a=[]
b=[]
for i=1:size(v)
if(isnumeric(v(i))
num=v(i)
a(x)=num
b(x)=1
for j=1:size(v)
if(v(i)==v(j))
b(x)=b(x)+1
v(j)='a'
end
end
v(i)='a'
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.