Index in position 2 exceeds array bounds?

11 ビュー (過去 30 日間)
越琪 吴
越琪 吴 2020 年 10 月 9 日
編集済み: KSSV 2020 年 10 月 9 日
I wrote the at the beginning of my code
jaywalk=zeros(1,n);
then I changed the values in jaywalk
at the end of my code, I wrote
for i=1:n
if jaywalk(1,i)==1
disp(plate(i));
end
end
when I run the code, matlab always tell me 'Index in position 2 exceeds array bounds'
how can i solve the problem?
Any help will be appreciated! Thanks!
  1 件のコメント
Fangjun Jiang
Fangjun Jiang 2020 年 10 月 9 日
what is the return value of size(plate)?

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

回答 (1 件)

KSSV
KSSV 2020 年 10 月 9 日
編集済み: KSSV 2020 年 10 月 9 日
m = length(jaywalk) ;
n = length(plate) ;
if m ~= n
error("length of jaywalk and plate should be same")
else
for i=1:n
if jaywalk(i)==1
disp(plate(i));
end
end
end
If dimensions of jaywalk and plate are same, you can striaght away use indexing instead of loop.
plate(jaywalk==1) % this will display value of plate when jaywalk == 1

カテゴリ

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