why do I get the error: Undefined function or variable 'a'.?
조회 수: 4 (최근 30일)
이전 댓글 표시
clear
clc
for m=6:3
a(m)=m^2
b(m)=m*s
end
a
b
댓글 수: 0
답변 (2개)
Star Strider
2017년 10월 3일
The problem is the way you index your for loop:
for m=6:3
The colon (link) operator default behaviour is to increment by 1 from the first value to the last. Here, the last value is lower than the first, so the loop limit is already satisfied at the start, and the loop does not execute and so does not calculate ‘a’ (or ‘b’). To modify that behaviour, set the increment to -1:
for m=6:-1:3
a(m)=m^2
b(m)=m*s
end
The loop now executes, although since ‘s’ is not defined here, will crash on the ‘b’ assignment line.
댓글 수: 0
Steven Lord
2017년 10월 3일
You're using the valArray syntax for the for keyword. That section of the documentation states the maximum number of times the loop will execute. Based on the information in that section of the documentation, what is that maximum number of loop executions for your example?
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!