Error DimensionMismatch in Julia - julia

Hi,
Learning a bit more about Julia (0.4.0), I am facing an interesting situation, probably with a simple solution that escapes me.
I have an array similar to this one:
17200x11 Array{Any,2}:
1 -16.449 -1.091 -3.6087 -12.6724 -1.5945 -14.7705 -7.2174 -25.2609 -3.7766 -14.3509
1 -16.6168 -5.2032 1.091 -3.8605 1.1749 -11.6653 -6.1264 -16.3651 -2.0142 -14.0991
1 -16.8686 -7.3853 3.8605 6.2103 -0.9232 -6.546 -8.1406 -10.0708 -2.2659 -16.3651
1 -16.5329 -10.4904 -1.7624 8.1406 -10.2386 1.3428 -16.0294 -6.4621 -4.6158 -19.5541
1 -13.8474 -13.5117 -13.6795 1.9302 -18.5471 3.6087 -22.995 -4.2801 -8.2245 -17.9596
1 -9.1476 -13.7634 -20.6451 -1.7624 -18.2953 1.091 -24.0021 -2.7695 -10.4904 -8.3923
1 -4.6997 -8.9798 -14.267 1.6785 -10.7422 1.1749 -19.3024 -2.2659 -11.0779 -2.6016
I have built a function like this one:
function aligner(mat,sc=schord)
ls=#parallel vcat for i=1:Int64(size(mat,1)/sc)
hcat(mat[((i-1)*sc+1),1],reshape(mat[((i-1)*sc+1):(i*sc),2:end],length(mat[((i-1)*sc+1):(i*sc),2:end]))') # reshape to convert array to vector and ' to transpose
end
return ls
end
Running this line
tmpU=aligner(tmpR,100)
I got this error:
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 1 got 100)")
in cat_t at abstractarray.jl:824
in hcat at abstractarray.jl:849
[inlined code] from none:3
in anonymous at no file:1500
in anonymous at multi.jl:684
in run_work_thunk at multi.jl:645
in remotecall_fetch at multi.jl:718
in remotecall_fetch at multi.jl:734
in anonymous at multi.jl:1485
in yieldto at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
in wait at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib (repeats 3 times)
in preduce at multi.jl:1489
[inlined code] from multi.jl:1498
in anonymous at expr.jl:1543
in aligner at none:2
Curiously, if I use only the core of the function (and of course, mat=myArray and sc=100), it works perfectly.
ls=#parallel vcat for i=1:Int64(size(mat,1)/sc)
hcat(mat[((i-1)*sc+1),1],reshape(mat[((i-1)*sc+1):(i*sc),2:end],length(mat[((i-1)*sc+1):(i*sc),2:end]))') # reshape to convert array to vector and ' to transpose
end
172x1001 Array{Any,2}:
1 -16.449 -16.6168 -16.8686 -16.5329 -13.8474 -9.1476 -4.6997 … 10.3226 3.273 -0.2518 4.364 7.2174 1.3428 -6.2103
1 -21.6522 -14.6866 -15.0223 -19.9738 -21.7361 -22.5754 -23.3307 12.1689 12.1689 8.0566 3.6926 3.0212 3.9444 1.3428
1 -6.6299 -4.6997 3.6926 7.5531 7.3013 4.1962 5.3711 -15.5258 -12.2528 -7.5531 -7.1335 -12.3367 -17.4561 -17.2882
1 9.903 5.9586 3.3569 4.1962 4.8676 4.6997 8.3923 0.9232 -0.5035 -5.9586 -9.9869 -9.6512 -1.7624 4.4479
1 19.1345 14.183 10.1547 10.4904 8.2245 2.4338 -3.6926 -4.8676 -6.7978 -8.8959 -11.5814 -15.0223 -11.0779 -3.1891
1 -3.1052 -0.7553 6.3782 6.2943 0.9232 0.8392 4.0283 … -8.0566 -8.5602 -9.5673 -10.6583 -8.0566 -2.2659 1.2589
I would appreciate any help to understand/solve the problem.
Kind Regards, RN

Well, it seems the solution is really simple:
function aligner(mat::Array,sc::Int=schord)
ls::Array=#parallel vcat for i=1:Int64(size(mat,1)/sc)
hcat(mat[((i-1)*sc+1),1],reshape(mat[((i-1)*sc+1):(i*sc),2:end],length(mat[((i-1)*sc+1):(i*sc),2:end]))') # reshape to convert array to vector and ' to transpose
end
return ls
end
!

Related

Lua: recursive function builts wrong table - pil4

while working on the exercise 2.2 of "programming in Lua 4" I do have to create a function to built all permutations of the numbers 1-8. I decided to use Heaps algorithm und made the following script. I´m testing with numbers 1-3.
In the function I store the permutations as tables {1,2,3} {2,1,3} and so on into local "a" and add them to global "perm". But something runs wrong and at the end of the recursions I get the same permutation on all slots. I can´t figure it out. Please help.
function generateperm (k,a)
if k == 1 then
perm[#perm + 1] = a -- adds recent permutation to table
io.write(table.unpack(a)) -- debug print. it shows last added one
io.write("\n") -- so I can see the algorithm works fine
else
for i=1,k do
generateperm(k-1,a)
if k % 2 == 0 then -- builts a permutation
a[i],a[k] = a[k],a[i]
else
a[1],a[k] = a[k],a[1]
end
end
end
end
--
perm = {}
generateperm(3,{1,2,3}) -- start
--
for k,v in ipairs (perm) do -- prints all stored permutations
for k,v in ipairs(perm[k]) do -- but it´s 6 times {1,2,3}
io.write(v)
end
io.write("\n")
end
debug print:
123
213
312
132
231
321
123
123
123
123
123
123

How to perform pandas drop_duplicates based on index column

I am banging my head against the wall when trying to perform a drop duplicate for time series, base on the value of a datetime index.
My function is the following:
def csv_import_merge_T(f):
dfsT = [pd.read_csv(fp, index_col=[0], parse_dates=[0], dayfirst=True, names=['datetime','temp','rh'], header=0) for fp in files]
dfT = pd.concat(dfsT)
#print dfT.head(); print dfT.index; print dfT.dtypes
dfT.drop_duplicates(subset=index, inplace=True)
dfT.resample('H').bfill()
return dfT
which is called by:
inputcsvT = ['./input_csv/A08_KI_T*.csv']
for csvnameT in inputcsvT:
files = glob.glob(csvnameT)
print ('___'); print (files)
t = csv_import_merge_T(files)
print csvT
I receive the error
NameError: global name 'index' is not defined
what is wrong?
UPDATE:
The issue appear to arise when csv input files (which are to be concatenated) are overlapped.
inputcsvT = ['./input_csv/A08_KI_T*.csv'] gets files
A08_KI_T5
28/05/2015 17:00,22.973,24.021
...
08/10/2015 13:30,24.368,45.974
A08_KI_T6
08/10/2015 14:00,24.779,41.526
...
10/02/2016 17:00,22.326,41.83
and it runs correctly, whereas:
inputcsvT = ['./input_csv/A08_LR_T*.csv'] gathers
A08_LR_T5
28/05/2015 17:00,22.493,25.62
...
08/10/2015 13:30,24.296,44.596
A08_LR_T6
28/05/2015 17:00,22.493,25.62
...
10/02/2016 17:15,21.991,38.45
which leads to an error.
IIUC you can call reset_index and then drop_duplicates and then set_index again:
In [304]:
df = pd.DataFrame(data=np.random.randn(5,3), index=list('aabcd'))
df
Out[304]:
0 1 2
a 0.918546 -0.621496 -0.210479
a -1.154838 -2.282168 -0.060182
b 2.512519 -0.771701 -0.328421
c -0.583990 -0.460282 1.294791
d -1.018002 0.826218 0.110252
In [308]:
df.reset_index().drop_duplicates('index').set_index('index')
Out[308]:
0 1 2
index
a 0.918546 -0.621496 -0.210479
b 2.512519 -0.771701 -0.328421
c -0.583990 -0.460282 1.294791
d -1.018002 0.826218 0.110252
EDIT
Actually there is a simpler method is to call duplicated on the index and invert it:
In [309]:
df[~df.index.duplicated()]
Out[308]:
0 1 2
index
a 0.918546 -0.621496 -0.210479
b 2.512519 -0.771701 -0.328421
c -0.583990 -0.460282 1.294791
d -1.018002 0.826218 0.110252

sql output as a list in ksh

I have a script where the sql output of the function is multiple rows (one column) and I'm trying to loop through those for loop function but can't get to seem to get it to work...
rslt=sqlquery {}
echo $rslt
1
2
3
4
for i in $rslt
do
echo "lvl$i"
done
but for the loop...I keep getting this back four times
lvl1
2
3
4
where as I want to get this back...
lvl1
lvl2
lvl3
lvl4
how do I get that?
In order to get the needed result in your script you need to take $rslt under double quotes ". This will ensure that you are not loosing the new lines \n from you result which you are expecting to have in the loop.
for i in "$rslt"
do
echo "lvl$i"
done
To loop over the values in a ksh array, you need to use the ${array[#]} syntax:
$ set -A rslt 1 2 3 4
$ for i in ${rslt[#]}
> do
> echo "lvl$i"
> done
lvl1
lvl2
lvl3
lvl4

fpc Pascal Runtime error 216 before execution ends

I was implementing adjacency list in Pascal (by first reading edge end points, and then using dynamic arrays to assign required amount of memory to edgelist of each node). The program executes fine, gives correct outputs but gives runtime error 216 just before exiting.
The code is :
type aptr = array of longint;
var edgebuf:array[1..200000,1..2] of longint;
ptrs:array[1..100000] of longint;
i,j,n,m:longint;
elist:array[1..100000] of aptr;
{main}
begin
readln(n,m);
fillchar(ptrs,sizeof(ptrs),#0);
for i:=1 to m do begin
readln(edgebuf[i][1],edgebuf[i][2]);
inc(ptrs[edgebuf[i][1]]);
end;
for i:=1 to n do begin
setlength(elist[i],ptrs[i]);
end;
fillchar(ptrs,sizeof(ptrs),#0);
for i:=1 to m do begin
inc(ptrs[edgebuf[i][1]]);
elist[edgebuf[i][1]][ptrs[edgebuf[i][1]]]:=edgebuf[i][2];
end;
for i:=1 to n do begin
writeln(i,' begins');
for j:=1 to ptrs[i] do begin
write(j,' ',elist[i][j],' ');
end;
writeln();
writeln(i,' ends');
end;
writeln('bye');
end.
When run on file
4 5
1 2
3 2
4 3
2 1
2 3
gives output:
1 begins
1 2
1 ends
2 begins
1 1 2 3
2 ends
3 begins
1 2
3 ends
4 begins
1 3
4 ends
bye
Runtime error 216 at $0000000000416644
$0000000000416644
$00000000004138FB
$0000000000413740
$0000000000400645
$00000000004145D2
$0000000000400180
Once the program says "bye", what is the program executing that is giving runtime error 216?
RTE 216 is in general fatal exceptions. GPF/SIGSEGV and in some cases SIGILL/SIGBUS, and that probably means that your program corrupts memory somewhere.
Compile with runtime checks on might help you find errors (Free Pascal : -Criot )

Get the first letter of a make variable

Is there a better way to get the first character of a GNU make variable than
FIRST=$(shell echo $(VARIABLE) | head -c 1)
(which is not only unwieldy but also calls the external shell)?
This is pretty horrible, but at least it doesn't invoke shell:
$(eval REMAINDER := $$$(VAR)) # variable minus the first char
FIRST := $(subst $(REMAINDER),,$(VAR)) # variable minus that
The GNU Make Standard Library provides a substr function
substr
Arguments: 1: A string
2: Start offset (first character is 1)
3: Ending offset (inclusive)
Returns: Returns a substring
I haven't tested it, but $(call substr,$(VARIABLE),1,1) should work
Since I came across this in my own search and didn't find what I was looking for here is what I ended up using to parse a hex number that could be applied to any known set of characters
letters := 0 1 2 3 4 5 6 7 8 9 a b c d e f
nextletter = $(strip $(foreach v,$(letters),$(word 2,$(filter $(1)$(v)%,$(2)) $v)))
then
INPUT := 40b3
firstletter := $(call nextletter,,$(INPUT))
secondletter := $(call nextletter,$(firstletter),$(INPUT))
thirdletter := $(call nextletter,$(firstletter)$(secondletter),$(INPUT))
etc.
It's ugly but it's shell agnostic

Resources