Andrés CM
Andrés CM

Reputation: 21

What method am I not using right?

I'm new to Julia and currently trying to run the following code:

Using DelimitedFiles

M=readdlm(data)

ts,A=M[:,1],M[:,2:end]

(nsweeps,N)=size(A)

dx=0.01;

x=[minimum(collect(A)):dx:maximum(collect(A))];

bx=[x-dx/2,x[end]+dx/2];

But, when I try to run the last line of code, it gives me the following error:

MethodError: no method 
matching(::Array{StepRangeLen{Float64,Base.TwicePrecision
{Float64},Base.TwicePrecision{Float64}},1}, ::Float64)

Closest candidates are:
-(!Matched::BigFloat, ::Union{Float16, Float32, Float64}) at 
mpfr.jl:437
-(!Matched::Complex{Bool}, ::Real) at complex.jl:307
-(!Matched::Missing, ::Number) at missing.jl:115

Can you please help me? Also, the data I'm using it's

30×6 Array{Float64,2}

UPDATE here's the whole function I'm trying to run is the following:

function mymain(filename,nsamples)
start_time=time()

M=readdlm(filename)
ts,A=M[:,1],M[:,2:end]
(nsweeps,N)=size(A)

dx=0.01;
x=[minimum(collect(A)):dx:maximum(collect(A))];
bx=[x-dx/2,x[end]+dx/2];
(bx,hA)=hist(A[:],bx);

f1=figure()
subplot(2,1,1); plot(ts,A,"-o"); xlabel("Time [ms]"); ylabel("Amps 
[mV]");

subplot(2,1,2); plot(x,hA,"-");  xlabel("Amps [mV]"); 
ylabel("Density");draw()

nparams=8             

Sx=Array(ASCIIString,1,nparams)     
Rx=zeros(2,nparams)         
nx=zeros(Int,1,nparams)         

Sx[1,1]="p";        Rx[1:2,1]=[0.02,0.98];      nx[1]=49
Sx[1,2]="n";        Rx[1:2,2]=[1,20];       nx[2]=20        
Sx[1,3]="tD";       Rx[1:2,3]=[50,200];         nx[3]=46
Sx[1,4]="a";        Rx[1:2,4]=[0.05,0.5];       nx[4]=46
Sx[1,5]="siga";     Rx[1:2,5]=[0.01,0.2];       nx[5]=39
Sx[1,6]="sigb";     Rx[1:2,6]=[0.01,0.1];       nx[6]=19
Sx[1,7]="tauf";     Rx[1:2,7]=[50,200];     nx[7]=46
Sx[1,8]="u1";       Rx[1:2,8]=Rx[1:2,1];        nx[8]=nx[1] 

x=zeros(maximum(nx),nparams)
p=zeros(maximum(nx),nparams)
dx=zeros(1,nparams)

for j=1:nparams
x[1:nx[j],j]=linspace(Rx[1,j],Rx[2,j],nx[j])'
dx[j]=x[2,j]-x[1,j]
end

S=zeros(Int,nsamples,nparams)      

sold=zeros(Int,1,nparams)
for j=1:nparams
sold[j]=rand(1:nx[j])
end

while x[sold[4],4]<=x[sold[5],5]    
sold[4]=rand(1:nx[4])
sold[5]=rand(1:nx[5])
end

while x[sold[8],8]<=x[sold[1],1]    
sold[1]=rand(1:nx[1])
sold[8]=rand(1:nx[8])
end

xold=zeros(1,nparams)
xnew=zeros(1,nparams)
for j=1:nparams
xold[j]=x[sold[j],j]
end
llold=myloglikelihood(xold,ts,A)    

for k=1:nsamples

snew=sold+rand(-1:1,1,nparams)   

if all(ones(1,nparams).<=snew.<=nx)            

allowed2=x[snew[4],4]>x[snew[5],5]        
allowed3=x[snew[8],8]>x[snew[1],1]         

if allowed2&allowed3

for j=1:nparams
xnew[j]=x[snew[j],j]
end

llnew=myloglikelihood(xnew,ts,A)     

if rand()<exp(llnew-llold)       
sold,llold=snew,llnew
end

end
end

S[k,:]=sold

end
for k=1:nsamples
for j=1:nparams
p[S[k,j],j]+=1/(nsamples*dx[j])
end
end 

f2=figure()
for j=1:nparams
subplot(2,4,j)
plot(x[1:nx[j],j],p[1:nx[j],j]);
xlabel(Sx[j])
end

diff_time=time()-start_time;
println("Total runtime 
",round(diff_time,3),"s=",round(diff_time/60,1),"mins." );

return S

end    

This goes in line with some other functions, but as you can see, this is the main function, so I really can't move forward without first runnning this one.

Upvotes: 0

Views: 76

Answers (1)

Colin T Bowers
Colin T Bowers

Reputation: 18560

It isn't clear what outcome you are hoping for here. So I'll just give some pointers that hopefully help.

First, in this line:

x=[minimum(collect(A)):dx:maximum(collect(A))];

the calls to collect are redundant. Also, I suspect you are trying to construct a StepRangeLen, but by putting it in [] you actually are getting a Vector{StepRangeLen}. I think what you want in this line is actually this:

x=minimum(A):dx:maximum(A);

Second, in this line:

bx=[x-dx/2,x[end]+dx/2];

note that dx/2 is a Float64 while x is a StepRangeLen. This is important because the latter is a collection so if you want to perform this operation element-wise across the collection you need to broadcast, that is, x .- dx/2. Note, I suspect you may not be on the latest version of Julia, because when I run this the error message actually tells me explicitly I need to broadcast. Anyway, in contrast, x[end]+dx/2 is fine and does not need to be broadcast because x[end] is Float64. So I think you want:

bx=[x .- dx/2, x[end] + dx/2];

Having said that, it isn't clear to me why you want this bx, which is why I said at the start I'm not sure what outcome you were hoping for.

Upvotes: 2

Related Questions