Omar
Omar

Reputation: 9

Calling an element of a vector inside a function in Matlab

I am new to Matlab and trying to define a simple function but keep running into an error. Details are:

1) V is a 31x1 vector;

2) The function mypi takes one input, which is a scalar (between 0 to 30). It finds the corresponding element in V vector and saves it in z.

3) Matrix A is a row vector with two elements 0 and z-10.

4) y, which is what I am interested in calculating is a linear function of the max of vector A.

Matlab, however, gives an error and is not recognizing element x in vector V. Can anyone please guide me how I should fix this problem? I will be grateful. Thank you.

function    y=mypi(x)
            z=V(x);
            A=[0, z-10];
            y=500+50*max(A);
end 

Upvotes: 0

Views: 141

Answers (1)

FangQ
FangQ

Reputation: 1544

you must pass V to mypi or make it visible to this function by defining it as global. But why bother passing both V and index x to this function instead of passing V(x) or z directly?

function    y=mypi(z)
            A=[0, z-10];
            y=500+50*max(A);
end

and call it by mypi(V(x))

Upvotes: 1

Related Questions