for loop or while loop?

조회 수: 11 (최근 30일)
K.G
K.G 2017년 11월 24일
답변: KSSV 2017년 11월 24일
Hi all, I have a matrix A = [2 3 7 13 24] and I want to form a loop: output = 0 for i=1:somehing! output = output+i also for specific numbers of i(for example for all odd i's) I want to have two outputs, one of them should be zero and the other one is the regular output of the loop for that i. how can I do this and I also want the loop to go through all the elements of A?

답변 (1개)

KSSV
KSSV 2017년 11월 24일
A = [2 3 7 13 24] ;
for i = 1:length(A)
if mod(i,2) % odd condition
[ i A(i)]
end
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by