Replacing the numbers in a matrix
1 回表示 (過去 30 日間)
古いコメントを表示
I have a matrix of numbers (22 x 22). Some of the numbers in the cells are zero. If the cell contains a zero, I want to replace in the numbers in the cells directly above, below, to the right and left also with a zero.
0 件のコメント
採用された回答
Jan
2017 年 4 月 11 日
編集済み: Jan
2017 年 4 月 11 日
Do you mean all 8 surrounding elements?
x = randi([0,5], 22, 22); % Example data
Z = conv2(double(x == 0), ones(3, 3), 'same');
x(Z ~= 0) = 0;
If you mean the 4 neighbors without the diagonals:
Z = conv2(double(x == 0), [0,1,0; 1,0,1; 0,1,0], 'same');
x(Z ~= 0) = 0;
その他の回答 (1 件)
Andrei Bobrov
2017 年 4 月 11 日
編集済み: Andrei Bobrov
2017 年 4 月 11 日
Let A - your array [22 x 22]
A(bwdist(A==0) <= 1) = 0
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!