can someone explain this code for me? i am not sure what the q is doing here inside the loop. why do both vectors after the loop have the same numbers?
조회 수: 1 (최근 30일)
이전 댓글 표시
a = rand(1,10);
b = zeros(1,10)
for q = 1:10
b(q) = a(q)
end
disp(a)
disp(b)
댓글 수: 0
답변 (2개)
Rohith Nomula
2020년 6월 15일
편집: Rohith Nomula
2020년 6월 15일
Basically first you are allocating space for 10 elements in b by doing
b = zeros(1,10)
After that for each element in b you are setting its value to a
here , b(1)=a(1), b(2)=a(2) .......
where q goes from 1 to 10 (its the index)
for q = 1:10
b(q) = a(q)
end
At the end you are displaying the a and b to make sure they both are the same .
댓글 수: 0
David Hill
2020년 6월 15일
q is the loop variable that goes from 1 to 10 in 1 step increments. q is used to index into both (a) and (b) arrays. The b array values are assigned the a array values; therefore, both arrays will contain the same values after the loop completes.
댓글 수: 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!