Create the following vector using a while loop

Hello, I am taking an introductory ENGR course this semester.
I am studying a while loop in MATLAB, however I don't understand how to create the code for a problem.
Create the following vector B = [4 8 12 16 20 24] Using a while loop. The while loop must create the vector and add an element to it each time it passes through the loop.
Could you offer me the answer with some comments?

답변 (1개)

KSSV
KSSV 2017년 11월 2일

1 개 추천

N = 6 ; % length of vector
% while loop
B = zeros(1,N) ;
B(1) = 4 ;
count = 1 ;
while count < 6
count = count+1 ;
B(count) = B(count-1)+4 ;
end
%%no loop
C = 4:4:24 ;
%%using for loop
D = zeros(1,N) ;
D(1) = 4 ;
for i = 2:N
D(i) = D(i-1)+4 ;
end

카테고리

도움말 센터File Exchange에서 루프와 조건문에 대해 자세히 알아보기

질문:

2017년 11월 2일

편집:

2017년 11월 2일

Community Treasure Hunt

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

Start Hunting!