Index in position 2 exceeds array bounds?
3 次查看(过去 30 天)
显示 更早的评论
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 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!