私はMatlabプロジェクトで作業しています。私はコード内のコメントに指示を記載しています。関数を呼び出すときに何も起こりません
%This function will take the number that you put in and give you a
%value
%of how many numbers it had to do the mathematical operations on to
%get to the number 1.
%If the number inputted is even, start by dividing it by 2. If the
%result is even, then repeat the process. If the result is odd, then
%multiply the result by 3 and then add 1. Keep doing this until the
%number is 1.
function [s, m] = collatz(num)
%INPUTS
%num = the number that the user inputs
%OUTPUTS
%s = the number of steps it took to reach the number 1
%m = the maximum value in that list
veclist = []; %Creates an empty vector which will hold the list of
%numbers
while num ~= 0 %While the number is not equal to zero
if num > 0 %If this number is greater than zero
if rem(num,2) == 0 %If the number is even
num = num/2; %divide the number by 2
veclist = [veclist, num]; %add the number to the vector
else %This says if the number is odd
num = (num*3) + 1; %Multiply that number by 3 and add 1
veclist = [veclist, num]; %Add that number to the list
end
end
end
s = length(veclist) %shows how many elements are in the vector
m = max(veclist) %shows the max value in the vector
end
関数を呼び出すときに何も起こらないことを誰かに教えてもらえますか?
私は「(5)collatzの」と言うと、それはあなたの元の質問への答えはcollatz.m
は、MATLABパス上ではなかったということです何も
アクティブなフォルダのようなサウンドが正しくない( 'pwd'を参照)か、ファイル名が間違っています(おそらく' collatz.m 'でしょう)。 –
@ Dev-iLアクティブなフォルダを変更するにはどうすればよいですか?私は私のファイル名をcollatz.m – user3011219
に設定しています。通常、 'cd'を使用して、例:' cd c:\ path \ to \ collatz'です。 –