display while loop output as an array
조회 수: 4 (최근 30일)
이전 댓글 표시
how do you display the output of a while loop as an array.
code:
function [] = hailstone_sequence(n)
n = input('Value for n: ');
h = 1;
while(n~=1)
if n==1
return
elseif mod(n,2)
n=3*n +1
else
n=n/2
end
h=h+1;
end
댓글 수: 2
채택된 답변
Walter Roberson
2017년 10월 5일
Just before the h=h+1 insert
output(h) = n;
댓글 수: 2
Walter Roberson
2017년 10월 5일
Then move it to after the while() statement.
But question: does the output need to include the 1? If so then make sure to add a 1 to the end before the return statement.
추가 답변 (1개)
jean claude
2017년 10월 5일
편집: jean claude
2017년 10월 5일
function [output] = hailstone_sequence(n)
output=[n];
h = 1;
while(n~=1)
if n==1
return
elseif mod(n,2)
n=3*n +1
else
n=n/2
end
h=h+1;
output= [output n];
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!