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!