当元素匹配/ matlab时,矩阵维度不会同意

时间:2014-04-21 18:57:52

标签: matlab

我在最终计算代码时出现问题,最后一部分,其中log是自然日志,我需要RD = facs。* log(log(facs))来划分sigmafac,或者robin = sigmafac ./RD。我的RD从1到100,我的sigmafac也是如此。为什么矩阵维度不匹配?

我希望RD的相应数字(numbas)除以sigmafac的对应数量,所有都具有相同的维度,所以我看不出问题的来源。我意识到RD(1)= - inf,这是导致问题的原因吗?以及如何修复它?

代码:

n=100;
primlist=2; % starting the prime number list


for numba=1:n;
   if mod(2+numba,primlist)~=0
      primlist=[primlist;2+numba]; %generating the prime number list
   end
end

fac=1; %initializing the factorials
RD=0;

for numbas=2:n

    %preallocating vectors for later use
    prims=zeros(size(primlist));
    pprims=zeros(size(primlist));
    pow=prims;

    for i=1:length(primlist) % identifying each primes in the primlist
        for k=1:10
            if mod(numbas,primlist(i).^k)==0
                prims(i)=primlist(i); % sum of all the powers of prims, such that prims divide numbas
                pow(i)=k; % collecting the exponents of primes
            end
        end

    if primlist(i)<=numbas
       pprims(i)=primlist(i); % primes less than or equal to numbas
    end

    end 


   % converting column vectors to row vector  
   PPRIMS=pprims';
   PRIMS=prims';
   POW=pow';


   %Creating the vectors
   PLN(numbas,:)=PPRIMS; % vector of primes less than or equal to number

   PPV(numbas,:)=PRIMS; % prime divisor vector

   PVE(numbas,:)=POW; % highest power of each primes for every number

   RVE=cumsum(PVE); % the cummulative sum of the exponents

   RVE(RVE~=0)=RVE(RVE~=0)+1; %selects each non zero element then add 1


   %factorial
   fac=fac*numbas;
   facs(numbas)=fac; %storing the factorials

   if facs==1
    RD==1; % log(log(facs1))) does not exist
   else RD=facs.*log(log(facs));
   end




end                


% setting up sum of divisor vector
NV=PLN.^RVE-1; % numerator part of sum of divisors vector
DV=PLN-1; % denominator part of sum of divisors

NV(NV==0)=1; % getting rid of 0 for elementwise product
DV(DV==-1)=1; % getting rid of -1 for elementwise product


sigmafac=prod(NV,2)./prod(DV,2); %sum of divisors


robin=(sigmafac)./(RD)

2 个答案:

答案 0 :(得分:1)

每当出现这样的错误时,首先应该检查

size(sigmafac)
size(RD)

在这种情况下,你会得到

ans =
   100     1
ans =
     1   100

所以它们的大小不一样。您需要进行一个或另一个的转置,然后您的部门将正常工作。

答案 1 :(得分:0)

您的sigmafac为100x1,但您的RD为1x100,产生错误。如果你想要这个只是改变

robin=(sigmafac)./(RD)

robin=(sigmafac)'./(RD)

这将使sigmafac成为1x100(转置),然后你的向量将具有相同的维度,你将能够进行除法。