嘿,我在这里遇到了一个奇怪的错误。这个函数只是找到一个数字的正确除数并返回它们。
function [divisors] = SOEdivisors(num)
%SOEDIVISORS This function finds the proper divisors of a number using the sieve
%of eratosthenes
%check for primality
if isprime(num) == 1
divisors = [1];
%if not prime find divisors
else
divisors = [0 2:num/2]; %hard code a zero at one.
for i = 2:num/2
if divisors(i) %if divisors i ~= 0
%if the remainder is not zero it is a divisor
if rem(num, divisors(i)) ~= 0
%remove that number and all its multiples from the list
divisors(i:i:num/2) = 0;
end
end
end
%add 1 back and remove all zeros
divisors(1) = 1;
divisors = divisors(divisors ~= 0);
end
end
我收到的错误是:
Integer operands are required for colon operator when used as index
它指的是第 23 行。
第 23 行是
divisors(i:i:num/2) = 0;
但我 i 和 num 都应该是整数......我知道 i 是一个整数。但即使我尝试
num = int8(num)
或类似的东西,我仍然得到错误。
谢谢阅读!