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
2017-11-2
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 中查找有关 루프와 조건문 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!