Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a matlab Function: [a, b] = index(v) which returns vectors a and b, given

ID: 3792622 • 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.

thanks, please try be as simple as can be.

Explanation / Answer

I am giving both functions index.m and find_index.m, for the evaluation purpose of the algorithm. You can delet find_index() function at the time of submission of the program

The function index.m

function [a,b] = index(v)
   N = length(v); % Number of numeric values in vector v
   a = []; b = [];% intial empty vector a and b
   for k =1:N % loop for each element in v
       ind = find_index(a,v(k)); % finding the index of v(k) in a
       if(ind == -1) %if empty
           a = [a,v(k)];% add v(k) to a
           b = [b,1];% make an entry in b
       else
           b(ind) = b(ind)+1; % if already added in a only increase b
       end
   end
end


function k = find_index(v,c)
   N = length(v);
   for k =1:N
       if(v(k) == c)
           return
       end
   end
   k = -1;
end

Testing the code

>> v = [1 4 3 4 5 4 3];
>> [a,b]=index(v)

a =

     1     4     3     5


b =

     1     3     2     1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote