필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by