嘿,我在这里得到一个奇怪的错误。这个函数只是找到一个数字的正确除数并返回它们。整数操作数错误
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;
但我我和num都应该是整数。 ..我知道我是一个整数。但即使当我尝试
num = int8(num)
或类似的东西,我仍然得到错误。
感谢您的阅读!
实际上,如果你强制num是int类型的,你不应该得到这个错误(也许是一个不同的错误)。见'int8(5)/ 2'。 ----旁注:如果你不需要,不要强迫matlab使用数字转换为整数,它可能会让你感到意外,通常不会被要求。 –