Hello All,
I wanted to ask if I can write some code to overcome the error resulting from index exceeding array bounds. For example, in the code below, is there anyth9ng that can be done to return a result x(11) = 0 instead of getting an error?
x=[1:10]
x(11)
Thanking you in advance.
Context: My issue is the array has 10 elements but I have several boundary condition equations trying go access element 11 (and element 0 as well). Hence I'm getting an error. So far I've been managing to avoid the error by modifying each single equation to account for the boundary condition. However I'm wondering if there's a general code I can write to get a value of 0 for indices outside the array boundaries.

 採用された回答

KSSV
KSSV 2020 年 7 月 30 日

0 投票

x=[1:11]
x(11)

3 件のコメント

Mizo Amreya
Mizo Amreya 2020 年 7 月 30 日
You misunderstood me. I'll try to add more to the question to make it clearer.
KSSV
KSSV 2020 年 7 月 30 日
x=[1:10] ;
idx = 11 ;
if idx>length(x)
x(idx) = 0 ;
end
x(11)
Mizo Amreya
Mizo Amreya 2020 年 7 月 30 日
Amazing. Thank you

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2020 年 7 月 30 日

コメント済み:

2020 年 7 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by