passiert nichts. Ich arbeite an einem Matlab-Projekt. Ich habe die Anweisungen in den Kommentaren im Code aufgelistet.Beim Aufruf der Funktion
%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
Kann mir jemand sagen, warum passiert nichts beim Aufruf der Funktion.
I „collatz (5)“ sagen und es gibt nichts
Klingt wie Ihr aktiver Ordner ist falsch (siehe 'pwd'), oder der Dateiname ist falsch (sollte wahrscheinlich' collatz.m') sein. –
@ Dev-iL Wie ändere ich meinen aktiven Ordner? Ich habe meinen Dateinamen als collatz.m eingestellt – user3011219
Im Allgemeinen mit 'cd', zB:' cd c: \ Pfad \ zu \ collatz'. –