a function that has two inputs: a number and a row vector

조회 수: 3 (최근 30일)
Eric Brown
Eric Brown 2022년 4월 9일
댓글: Steven Lord 2022년 4월 9일
The code i am tryin to make requires that there be only two inputs, the first a number and the second as a row vector. How do I do that? I have tried looking online and even in my textbook but i cant find anything.
I should calarify, when calling the function the command window should read My_func(1,[18 11 22 48]).
What I have in my .m file is function y = My_func(a,b,c,d,e), I believe this is where I am having a lack of knowlege.
  댓글 수: 2
Torsten
Torsten 2022년 4월 9일
function y = My_func(a,bcde)
b = bcde(1);
c = bcde(2);
d = bcde(3);
e = bcde(4);
end
Eric Brown
Eric Brown 2022년 4월 9일
God bless you.

댓글을 달려면 로그인하십시오.

답변 (2개)

Torsten
Torsten 2022년 4월 9일
편집: Torsten 2022년 4월 9일
scalar = 6.0;
vector = [3 5 8];
output = input(scalar,vector)
function output = input(scalar,vector)
output = scalar*vector;
end

KSSV
KSSV 2022년 4월 9일
function val = myfun(a,b)
if numel(a) ~= 1 | size(b,1) ~=1
error('check the inputs') ;
end
% Do what you want
end
  댓글 수: 1
Steven Lord
Steven Lord 2022년 4월 9일
Instead of using numel and size here, I recommend using isscalar and isrow or isvector (depending on whether you care about the orientation or just whether or not the input is a vector.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by