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)
?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Abr. de 2018
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.

Más respuestas (0)

Productos

Etiquetas

Preguntada:

el 10 de Abr. de 2018

Respondida:

el 10 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by