modify a row of an array within a function

17 ビュー (過去 30 日間)
Ernst Reißner
Ernst Reißner 2021 年 5 月 18 日
編集済み: James Tursa 2021 年 5 月 18 日
I have a 2 dimensional array arr and at various places i want to replace the ii-th row.
So I would like to write a function
function arr = replace(arr, ii)
and call that, e.g. in a loop but also with fixed ii.
My problem is that i fear that all of arr is copied then.
I just want a passed by reference and replace the row.
Is this feasable??
  1 件のコメント
Stephen23
Stephen23 2021 年 5 月 18 日
"My problem is that i fear that all of arr is copied then."
Have you checked this?

サインインしてコメントする。

回答 (2 件)

KSSV
KSSV 2021 年 5 月 18 日
If a is an array of size m*n, you can repalce i'th row with a row matrix b of size 1*n using:
a(i,:) = b ;
  1 件のコメント
Ernst Reißner
Ernst Reißner 2021 年 5 月 18 日
I know.
that is exactly what I would do within the function replace(...).
But my question is how i can avoid that when invoking replace all of a is copied
although within replace i just replace a single row.
I know, if I just read from a, it is not copied, but it is if i modify.
RIght now i wonder whether if i could define a to be an array of arrays,
accessing as a(ii)(jj).. this would spare someting. but far from ideal.

サインインしてコメントする。


James Tursa
James Tursa 2021 年 5 月 18 日
編集済み: James Tursa 2021 年 5 月 18 日
This can sometimes be done inplace depending on how you write the function and how you call the function. One requirement is your function needs to be called from within another function (easy to do). Another requirement is that your variable can't be shared with another variable (not easy to guarantee unfortunately). See this blog:

カテゴリ

Help Center および File ExchangePackage MATLAB Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by