Create the following vector using a while loop
Show older comments
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?
Answers (1)
KSSV
on 2 Nov 2017
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
Categories
Find more on 루프와 조건문 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!