Mack
Mack

Reputation: 675

Matlab Defining A Function That Has Vector As An Input

I was given a simple exercise by my professor, which is to define a cerrtain function. Here is my function

function [a,b,c] = PHUN([x,y],[z,t,w])

a = x
b = ((y+z)^2)*t 
c = z/w + x 

However, matlab states that I am using invalid syntax in the first line. So, I figured, perhaps there is a particular way in which vector inputs are supposed to be typed. I have attempt several searches on defining functions with vector inputs (or arguments), but have not been successful. I was wondering if someone could possibly help me.

Upvotes: 1

Views: 149

Answers (1)

Autonomous
Autonomous

Reputation: 9075

You can pass vectors as arguments in the same way you pass variables. Then access them in the function body appropriately. Your function could be re-written as follows:

function [a,b,c] = PHUN(X,Y)

a = X(1)
b = ((X(2)+Y(1))^2)*Y(2) 
c = Y(1)/Y(3) + X(1) 

Or if you want to keep the original variables:

function [a,b,c] = PHUN(X,Y)

Z = num2cell([X,Y]);
[x,y,z,t,w] = Z{:};

a = x
b = ((y+z)^2)*t 
c = z/w + x 

Upvotes: 1

Related Questions