フィルターのクリア

How to change an exact value of a submatrix from a given matrix ?

7 ビュー (過去 30 日間)
Virgilio Grigiotti
Virgilio Grigiotti 2018 年 2 月 7 日
回答済み: Alok Nimrani 2018 年 2 月 13 日
Hi all, I have this type of matrix 5x7:
A = [ NaN 2 6 6 10 11 NaN; 2 2 6 8 10 NaN ; 2 3 8 8 8 10 NaN ; 2 6 8 8 10 11 NaN; NaN 2 11 11 11 2 2]
I'd like to obtain this:
A = [ NaN *3* 6 6 10 11 NaN; *3* *3* 6 8 10 NaN ; *3* 3 8 8 8 10 NaN ; *3* 6 8 8 10 11 NaN; NaN *3* 11 11 11 2 2]
That is, I want to modify only a submatrix (5x2) of this matrix (5x7) by replacing with 3 all values equal to 2. Is it possible? Thank you in advance

回答 (1 件)

Alok Nimrani
Alok Nimrani 2018 年 2 月 13 日
Hi Virgilio,
Yes, it is possible to replace a specific value of a submatrix with a new value. For example, consider the matrix A as
A = [ NaN 2 6 6 10 11 NaN; 2 2 6 8 10 NaN NaN; 2 3 8 8 8 10 NaN; 2 6 8 8 10 11 NaN; NaN 2 11 11 11 2 2]
Now, a 5x2 submatrix of matrix A can be modified as follows to replace all occurrences of ‘2’ in this submatrix with ‘3’:
>> B = A(1:5,1:2); % select a 5x2 submatrix
>> B(B == 2) = 3; % find out all occurrences of ‘2’ in B and replace with ‘3’
>> A(1:5,1:2) = B; % write back the updated values to modify the original matrix

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by