Clear Filters
Clear Filters

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

7 views (last 30 days)
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

Answers (1)

Alok Nimrani
Alok Nimrani on 13 Feb 2018
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

Tags

Community Treasure Hunt

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

Start Hunting!