フィルターのクリア

Matlab functions with array

2 ビュー (過去 30 日間)
Muhtasim Ahmed
Muhtasim Ahmed 2018 年 3 月 30 日
コメント済み: Muhtasim Ahmed 2018 年 3 月 30 日
Let's say I have a function spline(n,u.v,w,x). How do I indicate that u,v, and w are arrays?

採用された回答

Walter Roberson
Walter Roberson 2018 年 3 月 30 日
There is no need to indicate that the variables are arrays. Each variable carries around its own information about data type and array size.
If you are expecting arrays, just be sure to use vectorized operators, such as .^ and .* and ./ instead of the matrix operators such as ^ and * and / -- unless, that is, you are doing matrix power, or algebraic matrix multiplication, or implicit matrix inversion.
If you happen to be working with symbolic variables, then there is no way to indicate that a symbol stands in for an array. The symbolic engine will always assume that any one symbolic name is a scalar quantity and that element-by-element arithmetic is to be preserved. For example if you have
syms A B
C = B * A
then this will expect scalar A and scalar B -- and chances are that it will rewrite it as
C = A * B
to satisfy its undocumented internal rules about the "pretty" way to write expressions.
If you have a symbolic expression, you can use matlabFunction() to convert it into an equivalent numeric function. When you do that, MATLAB will generate the code assuming element-by-element multiplication is to be done. For example it would turn the above C into
fun = @(A,B) A.*B
which would permit you to pass arrays into the anonymous function.
  1 件のコメント
Muhtasim Ahmed
Muhtasim Ahmed 2018 年 3 月 30 日
Thank you very much.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by