Borrar filtros
Borrar filtros

Matlab functions with array

2 visualizaciones (últimos 30 días)
Muhtasim Ahmed
Muhtasim Ahmed el 30 de Mzo. de 2018
Comentada: Muhtasim Ahmed el 30 de Mzo. de 2018
Let's say I have a function spline(n,u.v,w,x). How do I indicate that u,v, and w are arrays?

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Mzo. de 2018
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.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by