How to enter and define a function with the value of one input form the order of matrix for another input
2 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
采纳的回答
Walter Roberson
2021-9-1
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.
更多回答(1 个)
Bjorn Gustavsson
2021-9-1
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!