⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updown4.m

📁 《精通matlab7》“mastering matlab 7”的代码。
💻 M
字号:
% updown4.m
% up-down algorithm


if ~exist('Nums','var')     % numbers to test
   Nums = 25:50;
end
N = Nums;                % duplicate numbers
Counts = zeros(size(N)); % preallocate array

not1 = N>1;                      % True for numbers greater than one

while any(not1)
   
   odd = rem(N,2)~=0;            % True for odd values
   
   odd_not1 = odd & not1;        % True for odd values greater than one
   even_not1 = ~odd & not1;      % True for even values greater than one
   
   N(even_not1) = N(even_not1)/2;           % Process evens
   Counts(even_not1) = Counts(even_not1)+1;
   
   N(odd_not1) = (3*N(odd_not1)+1)/2;       % Process odds
   Counts(odd_not1) = Counts(odd_not1)+2;
   
   not1 = N>1;                   % Find remaining numbers
end
%results=[Nums' Counts']

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -