Index error in code

3 ビュー (過去 30 日間)
Abdullah Qayyum
Abdullah Qayyum 2020 年 4 月 18 日
回答済み: Dhruv 2025 年 6 月 2 日
I am trying to execute this code but it gives me an error Index exceeds array bounds.
I= [234, 112, 22, 56; 78, 81, 119, 121; 98 193 250 211; 33 135 199 201];
s=[110 45 32; 95 150 67; 89 76 55];
s2= s(I+1)
  2 件のコメント
KSSV
KSSV 2020 年 4 月 18 日
What you are doing is wrong. You are trying to access more number of elements from s. What exactly you want to do?
Abdullah Qayyum
Abdullah Qayyum 2020 年 4 月 19 日
I am applying sbox on an encrypted image.

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

回答 (1 件)

Dhruv
Dhruv 2025 年 6 月 2 日
The error is being encountered because you're using values from matrix I to index into matrix s, which only has 9 elements (3x3 matrix). MATLAB uses 1-based indexing, so the valid index range for s is 1 to 9. However, elements in I+1 go well beyond this range (up to 251), causing the "Index exceeds the number of array elements" error.
To fix it, either make s a vector with at least max(I(:)) + 1 elements or ensure the values in I+1 do not exceed 9.
I hope this helps!

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by