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

 採用された回答

Walter Roberson
Walter Roberson 2018 年 4 月 10 日

0 投票

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.

その他の回答 (0 件)

カテゴリ

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by