How to change an exact value of a submatrix from a given matrix ?
16 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!