Write a matlab Function a b indexv which returns vectors a

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)

Solution

function [a,b] = index(v)

l = 0 ;

for k = 1:length(v)

if k==1

l=l+1;

a(l) = v(k);

elseif ~ismember(a,v(k))

l = l + 1;

a(l) = v(k);

end

end

len = length(a);

for i=1:len

cmp = a(i);

count = 0;

for j = 1:length(v)

if cmp == v(j)

count = count + 1;

end

end

b(i) = count;

end

end

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 vec

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site