*Y=2;
g=[20,10];
w=40 : -2 : 3;
for x=1:length(w)
if w(x)<=5 "why get I problem here, its says 'Index exceeds matrix dimensions'"
break
else
w=setdiff(w(w>(5)),g);
A= x*3;
w1=A.*Y;
end
end*

 채택된 답변

Birdman
Birdman 2018년 3월 14일
편집: Birdman 2018년 3월 14일

0 개 추천

Because at the following line
w=setdiff(w(w>(5)),g)
your w vector becomes a 16 element array when x=1 and when x=17, you try to reach 17th element of your w array, which is not possible.
If you put a counter to your loop named cnt as follows, you will see the reason actually:
Y=2;
g=[20,10];
w=40 : -2 : 3;
cnt=0;
for x=1:length(w)
cnt=cnt+1;
if w(x)<=5 "why get I problem here, its says 'Index exceeds matrix dimensions'"
break
else
w=setdiff(w(w>(5)),g);
A= x*3;
w1=A.*Y;
end
end
When the loop is broken, the value of cnt is 17.

댓글 수: 4

Y=2;
g=[20,10];
w=40 : -2 : 3;
for x=1:length(w)
if w(x)<=5
break
else
w=setdiff(w(w>(5)),g); > I JUST WANNA w to SKIP THE VALUES OF g. HELP!
A= x*3;
w1=A.*Y;
end
end
Birdman
Birdman 2018년 3월 14일
편집: Birdman 2018년 3월 14일
Ok, you don't need to do it in the loop, because you overwrite w at the very first step and actually that line is just a waste of time until loop is broken. You get rid of g values at the first step. You may do it outside the loop.
silvano carvalho
silvano carvalho 2018년 3월 14일
Birdman allways saves
Birdman
Birdman 2018년 3월 14일
Haha, you are welcome :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2018년 3월 14일

댓글:

2018년 3월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by