How to define a matrix of variables in matlab as function ?

Status
Not open for further replies.
I'm not sure exactly what you are asking for. My interpretation is that you want to write a function that returns a matrix. I have some doubt because I think that maybe you are looking to define a matrix with some variables in it, and you want the matrix to be updated whenever you update the variables.

For either of those, perhaps you can do something like this:

In junk.m
Code:
function B = junk (a,b,c,d)
   B = [a, b ; c, d];
endfunction

You can call the function with the input variables wherever you would have been using the matrix variable that you were using.
 

thank but i mean tha i have
my function: for exemple matrix (1x7) zise
one var x for exemple cost in (1x7) zise and i want choose this matrixe from data
for exemple i have 7 cost and i want chose 5 best cost
how i define this matrix and how choose the best 5 cost if i give sum of my cost for exemple equal 20 dollars oki
 
Last edited:

You can define a variable-size matrix by using a constructor with nonconstant dimensions. For example:

function y = var_by_assign(u) %#codegen
if (u > 0)
y = ones(3,u);
else
y = zeros(3,1);
end
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…