Make a function accept vector
7 views (last 30 days)
Hello, I am trying that this function below accepts vector inputs, but it doesnt want to work.
function y = myfunction(x)
I tried to replace the x* with x.*, but it wont work either. Maybe I am calling it wrong? myfunction(5) works, but myfunction([5 2]) does not.
Thanks in advance!
KL on 12 Mar 2018
This is called the element-wise operation. When you write x^2, MATLAB tries to find x*x but when you write x.^2, it calculates square of every element.
>> x = 1:3;
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
1 4 9