How to declare input variables as a vector?

Given
function [y1,...,yN] = myfun(x1,...,xM)
In matlab is it possible that the input variable x1 is a vector?

 採用された回答

cr
cr 2021 年 9 月 7 日

0 投票

Of course it can be a vector, a matrix or a variable of some class.

6 件のコメント

Walter Roberson
Walter Roberson 2021 年 9 月 7 日
Right. In MATLAB itself you do not have to declare any class or size information for parameters.
These days it is possible to add in information about class and size for argument validation purposes . That information is used at the time of calls to the function to verify that the parameters are valid, but it is mostly not reasoned about
Walter Roberson
Walter Roberson 2021 年 9 月 7 日
The rules change a bit once you start using Simulink or MATLAB Coder
cr
cr 2021 年 9 月 8 日
I agree. Thanks for the note.
Fidele Adanvo
Fidele Adanvo 2021 年 9 月 10 日
This means that in my .m I can call the function like this
[y1,....~]=myfun(x1,.....). where x1 is a vector?
for example x1=linspace(1,100).
Steven Lord
Steven Lord 2021 年 9 月 10 日
Sure, why not?
x = 0:360;
y = sind(x); % The input to the sind function is a vector
plot(x, y)
If you have a function that accepts multiple inputs and you want to pass in a vector and have each element treated as a separate input, that may not be as easy. As an example the hypot function requires two inputs:
z1 = hypot(3, 4)
z1 = 5
If you had the vector [3 4] and wanted to pass it into hypot as two separate inputs, the easiest way to do that would be to do it explicitly.
v = [3 4];
z2 = hypot(v(1), v(2))
z2 = 5
Some functions can accept either a vector or individual elements. Functions like ones are a prime example of this.
A1 = ones(3, 4)
A1 = 3×4
1 1 1 1 1 1 1 1 1 1 1 1
A2 = ones([3, 4])
A2 = 3×4
1 1 1 1 1 1 1 1 1 1 1 1
Fidele Adanvo
Fidele Adanvo 2021 年 9 月 10 日
Now I understand why my code gave an error.
Thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

質問済み:

2021 年 9 月 7 日

コメント済み:

2021 年 9 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by