A function that updates the input

Very basic question. Can I define a function that updates a matrix A with another entry B. That is
function A=Update(A,B)
?

 Risposta accettata

Yes, provided that the array to be updated is an output as well as an input, and provided that the user assigns the output on top of the same array. For example,
pqr = randi(5,2,3);
rst = randi([-2 2], 2, 3);
pqr = Update(pqr, rst);
function A = Update(A,B)
A = A+B;
end
However, what you cannot (typically) do is something like
pqr = randi(5,2,3);
rst = randi([-2 2], 2, 3);
Update(pqr, rst);
and expect that pqr will be changed by the Update function. It is not always impossible, but it is not good programming.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by