How to modify array in modifying subarray reference ?

4 ビュー (過去 30 日間)
Clément T
Clément T 2020 年 4 月 28 日
編集済み: Ameer Hamza 2020 年 4 月 28 日
Hello,
I would like to be able to modify an array in modifying a subarray. For example, I have these 3 arrays :
a = [1; 2];
b = [3 5; 4 6];
c = [a b];
When I modify a column of b,
b(:,1) = [6 8];
I would like that c changes too and becomes : [1 6 5; 2 8 6]. Whereas currently c remains [1 3 5; 2 4 6]...
How can I do that ?

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 28 日
編集済み: Ameer Hamza 2020 年 4 月 28 日
There is no easy way to do such a thing in MATLAB. The closest you can get is to define your own class and add event listeners to react whenever there is a change in the value of b. That will require a fair share of understanding of MATLAB OOP. Read Siddharth's answer here: https://www.mathworks.com/matlabcentral/answers/808-how-to-react-on-a-change-of-variable-from-workspace to get a general idea. Also, read the following resources
  1 件のコメント
Clément T
Clément T 2020 年 4 月 28 日
Thank you for your answer. I'll go check it out

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

その他の回答 (1 件)

Saurav Roy
Saurav Roy 2020 年 4 月 28 日
Hi....
I am not sure I understood your question properly but I wrote something as per my initial understanding to get your results.
a = [1; 2];
b = [3 5; 4 6];
b(:,1) = [6 8];
c = [a b];
disp(c);
  1 件のコメント
Clément T
Clément T 2020 年 4 月 28 日
Thank you for your answer.
After I create a and b, I'll define c as :
c = [a b]
I was asking if there is a way to change the second column of c without doing :
c(:,2) = [6 8]
but in doing :
b(:,1) = [6 8]
In other words, is Matlab able to understand that b is in c ? And so if modifying b modifies c ?

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

カテゴリ

Help Center および File ExchangeArray Geometries and Analysis についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by