MATLAB Answers

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

조회 수: 2(최근 30일)
reza Shahzadi
reza Shahzadi 2021년 9월 1일
댓글: reza Shahzadi 2021년 9월 2일
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)

채택된 답변

Walter Roberson
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
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))));
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).


Community Treasure Hunt

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

Start Hunting!

Translated by