i need a for loop that outputs certain numbers

조회 수: 1 (최근 30일)
Mahmoud Chawki
Mahmoud Chawki 2022년 5월 16일
댓글: Mahmoud Chawki 2022년 5월 16일
i want a loop that does the following:
first it gives me the 2 numbers which are 1 and 2
then it loops again and gives me the 2 number 3 and 4
then it loops again and gives me the 2 numbers 5 and 6
  댓글 수: 1
Jon
Jon 2022년 5월 16일
what do you mean "gives me", do you just want to display the number on the screen, do you want it saved in array?

댓글을 달려면 로그인하십시오.

채택된 답변

Jon
Jon 2022년 5월 16일
편집: Jon 2022년 5월 16일
incr = 2; % increment value
numIter = 3; % number of iteration
x = 1; % initial value
X = zeros(numIter,2); % array to hold values
for k = 1:numIter
X(k,:) = [x,x+1];
% display the values
disp(X(k,:))
% increment the value
x = x + incr;
end
X
  댓글 수: 2
Jon
Jon 2022년 5월 16일
편집: Jon 2022년 5월 16일
You can also do this without any loops
x = 1:6
X = reshape(x,2,3)' % note reshape put elements in columnwise, so transpose to get 3 by 2
Mahmoud Chawki
Mahmoud Chawki 2022년 5월 16일
thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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