Create the following vector using a while loop
Ältere Kommentare anzeigen
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?
Antworten (1)
KSSV
am 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
Kategorien
Mehr zu 루프와 조건문 finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!