how to use a vector as an input in a function

조회 수: 72 (최근 30일)
saeed rezaei
saeed rezaei 2019년 3월 31일
답변: Star Strider 2019년 3월 31일
dear all
i wanna creat a function which use a vector and a digit as inputs. it is as below:
function [z]=(x,[m,n,o,p])
z=x+m+n+o+p;
end
but it doesn't work!
how can i use a vector as inupt?
thanks alot

채택된 답변

Star Strider
Star Strider 2019년 3월 31일
See the documentation on Function Basics (link).
Your code has a number of errors.
This corrected version works:
function [z]=f(x,v) % Define & Code Function ‘f’
z = x+v(1)+v(2)+v(3)+v(4);
end
z = f(42,[5 12 3 7]) % Call Function ‘f’
I will let you explore the reasons it does.
Note that if you are simply summing the elements of vector ‘v’, use the sum function. However, since I believe this is not your actual problem, I am summing the elements individually.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by