フィルターのクリア

how to ignore an index error?

2 ビュー (過去 30 日間)
Laura Alvarez
Laura Alvarez 2019 年 12 月 14 日
編集済み: Laura Alvarez 2019 年 12 月 14 日
I have this code and I want to ignore the error when the index exceeds array bounds or when they are 0.
for j=1:N
for i=1:N
u(i,j)=(c(i,j)+u(i-1,j)+u(i+1,j)+u(i,j-1)+u(i,j+1))/4
end
end
I just want to ignore u(0, j), u(N + 1, j), u(i, 0) and u(i, N + 1) or assign them the value 0.
Thank you so much!
  1 件のコメント
dpb
dpb 2019 年 12 月 14 日
編集済み: dpb 2019 年 12 月 14 日
There is no 0-index array element so assigning it any value, zero or not, is out of the question.
You could put the expression in a try...catch...end construct but reconstructing the expression to maintain arrays in bounds would be the better solution.
filter2() possibly would be of use here altho the expression appears somewhat unusual with five terms dividing by four...

サインインしてコメントする。

採用された回答

dpb
dpb 2019 年 12 月 14 日
H=[0 1 0;1 0 1; 0 1 0]; % filter coefficients
u(2:end-1,2:end-1)=(c(2:end-1,2:end-1)+filter2(H,u,'valid'))/4; % apply filtered u to c

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by