How to enter and define a function with the value of one input form the order of matrix for another input

1 visualización (últimos 30 días)
I want to define a function with a couple of input while the value which devoted to one parameter is the order of a matrix, which is another input for that function.
As it is observed, inputs for function "opt" are x, y and [A], and the order of matrix [A] is 1*x.
I really appreciate to have your instructions
function[PS]=opt(x, y, [A]1*x)

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Sept. de 2021
MATLAB does not care. Just do
function PS = opt(x, y, A)
If you want, you can add in
assert(isscalar(x) && size(A,1) == x)
or other size validation code to check that the relationship holds.
MATLAB is not C, that does not know the size of an array unless you pass that information in. You can just ask for size(A) and MATLAB will know what all dimensions of the array are.

Más respuestas (1)

Bjorn Gustavsson
Bjorn Gustavsson el 1 de Sept. de 2021
If you need a function with a bit more complexity than "a one-liner" simply write it as an m-function file. Then you can do pretty much any complicated operations that can be programmed. Something like this should illustrate your case:
function PS = your_opt(x,y,A,q)
[U,S,V] = svd(A);
szS = size(S);
PS = U*x + y*S(max(1,min(q,szS(1))),max(1,min(q,szS(2))));
end
If you can perform, or write, the operations you want made on your matrix A to combine with x and y you can implement it in a matlab-function. Just remember to put it in a directory that is on your matlab-path (but not in the directories of your matlab-installation).
HTH

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by