2014-04-21 299 views
0

我在我的代碼的最後計算,最後一部分,其中日誌是自然日誌,我需要RD = facs。* log(log(facs) )來劃分sigmafac,或robin = sigmafac。/ RD。我的RD從1到100,我的sigmafac也是如此。爲什麼矩陣尺寸不匹配?矩陣尺寸不會同意當元素匹配/ matlab

我希望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) 

回答

0

您的sigmafac是100x1但您的RD是1x100這是產生錯誤。如果你想要這個工作只是改變

robin=(sigmafac)./(RD) 

robin=(sigmafac)'./(RD) 

這將使sigmafac一個1×(轉),然後你的載體將具有相同的尺寸,你將能夠做到分工。

+1

我不知道我能多謝謝你,我真的很感激它!謝啦! – Crisp

1

當你得到這樣的錯誤,你的第一次檢查應該是測試

size(sigmafac) 
size(RD) 

在這種情況下,你會得到

ans = 
    100  1 
ans = 
    1 100 

所以它們的大小不一樣。你需要轉換一個或另一個,然後你的部門將正常工作。

+0

感謝您的幫助 – Crisp