How to read formatted file without delimiters in matlab? - matlab

I have a file with rows like this (first two rows with 25 columns in each):
1921.300 . . < 0.030 . . . . . 550 1.6 1 Mrr1922 Jm 5
1973.220 158. 3. 0.240 0.002 . . 1.5 0.5 620 5.1 1 Lab1974 S 4
and description (first 4 columns as example):
term columns format description
date 008-017 f10.5 Observation date, in years.
tflag 019-019 a1 Flag for theta (position angle) measure. Flags
include:
: = uncertain/estimated (old code U)
L = originally published as nf, sp, etc.
(old code L)
Q = quadrant flipped 180deg from published value
(old code Q)
V = measure is vector separation along this angle
vector (previously used only in
interferometric catalog)
theta 020-026 f7.3 position angle, in degrees
terr 028-033 f6.3 published formal theta error, in degrees
etc.
-----------------------------------------------------------------------------
How to read this file in Matlab? I have no delimiters (only positions and formats). The basic idea is to read this file in my SQLite database but (if I right) SQLite works only with delimiters.

Related

Cannot find a proper Huffman algorithm

I am currently writing a code for Huffman encoder for 5 symbols, I have built the complete tree (adding and sorting the symbols and storing the locations at each stage), but the problem is that I cannot get a relation between the locations of the symbols and the zeros and ones, so can any one help me in finding this relation to build the code words for each symbol?
A=[0.15 0.1 0.4 0.3 0.05];;
B = sort (A, 'descend');
[B,M1]=sort(B,'descend');
len= length (A);
Location = cell(1,len-2);
for n = 1:len-2
T=B(len)+B(len-1);
X=[len, len-1];
B(X)=[];
new_len=length(B);
B(new_len+1)=T;
[B,M]=sort(B,'descend');
Location{n} = M ;
len=length(B);
end

Gnuplot find local maximum of 3D data

I have a problem in gnuplot...
I am making an splot from my data points which are discrete "lines" (see attached pic) among y values of 1,1/2,1/3 etc...
At every discrete "line" I would like to get the maximum Z value and its X and Y coordinates, and highlight them, or maybe fitting a function on them etc...
Here is my code:
set title "1/m vs mutation rate"
#set term pdfcairo size 6,4
set term x11
set xlabel "Mutation rate"
set ylabel "1/m"
set xrange[0.0001:0.05]
set yrange[1.0/30:1]
unset log x
set cbrange[0:0.35]
set zrange[0:1]
set palette defined ( 0 "green", 1 "blue", 2 "red")
#set view 78,348,1,1
set view map
set output "muemmeres500map.pdf"
splot 'muemmeres500.txt' u 1:2:3 with points pt 5 ps 1 palette, "muemmeres500.txt" every 30 using ($3==GPVAL_DATA_Z_MAX?$1:NaN):($3==GPVAL_DATA_Z_MAX?$2:NaN):3 title "max1" lc rgb'black' lw 4, "muemmeres500.txt" every 30::2 using ($3==GPVAL_DATA_Z_MAX?$1:NaN):($3==GPVAL_DATA_Z_MAX?$2:NaN):3 title "max2" lc rgb'black' lw 4, "muemmeres500.txt" every 30::3 using ($3==GPVAL_DATA_Z_MAX?$1:NaN):($3==GPVAL_DATA_Z_MAX?$2:NaN):3 title "max3" lc rgb'black' lw 4, "muemmeres500.txt" every 30::4 using ($3==GPVAL_DATA_Z_MAX?$1:NaN):($3==GPVAL_DATA_Z_MAX?$2:NaN):3 title "max4" lc rgb'black' lw 4, "muemmeres500.txt" every 30::5 using ($3==GPVAL_DATA_Z_MAX?$1:NaN):($3==GPVAL_DATA_Z_MAX?$2:NaN):3 title "max5" lc rgb'black' lw 4
unset output
And here is the data file: http://pastebin.com/umqGWtyy
As you can see in the picture, the "lines" data points correspond to each line in the datafile, so for instance the data points starting with the first then every 30 correspond to the "line" which has a y value 1, then from the second line every 30 corresponds to the "line" which y value is 1/2 etc...
Therefore I wanted to get the maximum Z value from just those data...
I tried sed as well, but I failed...
So my problem is, that it can just find the global maxima and not the other local ones...:( pls help me:)
Here is the picture:
I have no idea... hope it is understandable and sorry for my english...:)
GPVAL_DATA_Z_MAX doesn't seem to work for your problem but you can use stats instead to find all the local maxima and then plot them all in a looped plot.
#Do it before setting the ranges (the column will be handled as an x column and it might get out of xrange)
do for [i=0:28]{
#Give an indexed prefix to each stat (so they *all* become accessible from outside the loop, like "A12_max" or "A25_min")
stats 'muemmeres500.txt' every 30::i u 3 nooutput prefix "A".i
}
#set all the things you need for the plot (including ranges)
...
splot 'muemmeres500.txt' u 1:2:3 with points pt 5 ps 1 palette, \
for [i=0:28] '' every 30::i u 1:2:($3==value("A".i."_max") ? $3 : NaN) notitle #t "Max".(i+1)
Note: the indices used by every start from zero.
This only works for plotting, you have all the maxima but you don't have the X and Y coordinates yet.
You also have the indices of the maxima so if you can retrieve the X Y values from the A<n>_index_max row (actually its 30*index+i or the nth block's ith row ) you have the nth maximum position. To retrieve the nth row you can use stats again with every.
do for [i=0:28]{
stats 'muemmeres500.txt' every ::i:value("A".i."_index_max"):i:value("A".i."_index_max") u 1:2 nooutput prefix "P".i
}
If you do this right after getting the Ai_ stats you already have all the positions P<i>_max_x P<i>_max_y and Z values A<i>_max.
If you want you can print them to a file:
set print "maxima.dat"
do for [i=0:28]{
print value("P".i."_max_x"), (value("P".i."_max_y")), (value("A".i."_max"))
}
unset print

Visio NURBS formula

I have trouble deciphering the individual parameters of the NURBS formula in the shapes NURBSTo entry (used for splines - curved edges). MS Visio documentation didn't help too much.
The number of parameters is variable depending on the complexity of the curve. A more simple example is:
NURBS(0.4492,3,0,1,0,-0.1875,0,1,1,-0.1875,0,1)
where I found out the start and end coordinate parameters start is 5th for X and 6th for Y. End is 9th for X and 10th for Y. But the Y coordinates are still wrong, so I suppose they should be combined with another parameter. This Java code provided the best results so far in getting the control points of the spline:
int j = 0;
for (int i = 2; i + 4 < pointsS.length; i = i + 4)
{
mxPoint currPoint = new mxPoint();
currPoint.setX(startXY.getX() + (endXY.getX() - startXY.getX()) * pointsRaw[i + 2]);
currPoint.setY(startXY.getY() - (endXY.getY() - startXY.getY()) * pointsRaw[i + 3]);
pointList.add(currPoint);
j++;
}
Just an example for a more complex spline:
NURBS(2.9857,3,1,1,0.1875,0,0,1,0.1875,-0.8954,0,1,0.1875,-1.3431,0,1,0.1875,-1.7908,0.4521,1,-0.4936,-1.7908,1.049,1,-1.1747,-1.7908,1.424,1,-1.1747,-2.1799,1.902,1,-1.1747,-2.5689,2.3742,1)
The documentation say for param 2 only "degree". I suppose it's the degree of the polynomial that is used for approximation.
The wiki page about NURBS:
http://en.wikipedia.org/wiki/Non-uniform_rational_B-spline
Of course, it doesn't speak about Visio parameters :)
Are you accounting for the effect third and fourth parameters have on how you should interpret the x and y parameters?
From MSDN (http://msdn.microsoft.com/en-us/library/office/aa224197(v=office.11).aspx):
NURBS(knotLast, degree, xType, yType, x1, y1, knot1, weight1, ...)
knotLast The last knot.
degree The spline's degree.
xType Specifies how to interpret the x input data. If xType is 0, all x input data are interpreted as a percentage of Width. If xType is 1, all x input data are interpreted as local coordinates.
yType Specifies how to interpret the y input data. If yType is 0, all y input data are interpreted as a percentage of Height. If yType is 1, all y input data are interpreted as local coordinates.
x1 An x-coordinate.
y1 A y-coordinate.
knot1 A knot on the B-spline.
weight1 A weight on the B-spline.
This may help: Visio 2003 Developer's Survival Pack by Graham Wideman
http://www.amazon.com/Visio-2003-Developers-Survival-Pack/dp/1412011124
There's an extensive section on Visio NURBS. It's only $7 for the Kindle edition.

gnuplot: Is it possible to plot the (x, y) coordinate besides each point?

I found an answer here about using labels with the points but that only works for 1 column with the label, whereas in order to plot (x, y) along with the point I have to use 2.
So I need something along the lines of plot "data.txt" using ($1):($2):1:2 with labels, "data.txt" using 1:2 with ($1):($2):1:2 being (x):(y) the coordinate for the point on the image and 1:2 being the label, except that doesn't work since it isn't a valid syntax and so only the first value is plotted at the correct location.
Well I guess I can use something like plot "data.txt" using ($1+2):($2+2):1 with labels, "data.txt" using ($1+10):($2+2):2 with labels, "data.txt" using 1:2 to manually set the spacing but damn is that ugly/low-level/hackish/bad/etc.
how about this:
set key off
get_point(x,y) = sprintf("(%.0f,%.2f)", x, y)
offset(y) = (y<0.5) ? (y - 0.05) : (y + 0.05)
plot [-1:6] "-" u ($1):(offset($2)):(get_point($1, $2)) with labels, "-" u 1:2 w l
0 0.20323
1 0.19147
2 0.50213
3 0.17599
4 0.07732
5 0.66897
e
0 0.20323
1 0.19147
2 0.50213
3 0.17599
4 0.07732
5 0.66897
e
What it does:
With the get_point macro you can easily form a string which will be your x- and y-coordinates. The offset function is simply to move the labels a bit away from the curve. This might be useful if you data has a specific form (like in this case a global mean value of 0.5).
Of course the script looks nicer, if you use a datafile instead of having the data in the plot file.

Path integral in Matlab

I know that doing Feynman path Integral on Matlab is time consuming compare to Fortran or C.
However, do someone have a Matlab code of harmonic oscillator via path integral?
I didn't manage to find any on the web (and even on Matlab forum).
Below a Fortran code which I don't know how to translate to Matlab (I am novice)
Thanks, Joni
! qmc . f90 : Feynman path i n t e g r a l for ground s t a t e wave Function
Program qmc
Implicit none
Integer :: i,j , max , element , prop ( 100 )
Real *8 :: change , ranDom , energy , newE , oldE , out , path ( 100 )
max = 250000
open ( 9 , FILE = ’qmc.dat’ , Status = ’Unknown’ )
! initial path and probability
Do j = 1 , 100
path (j) = 0.0
prop (j) = 0
End Do
! find energy of initial path
oldE = energy(path , 100)
! pick random element , change by random
Do i = 1 , max
element = ranDom ( )*100 + 1
change = ((ranDom() - 0.5)*2)
path (element) = path(element) + change
newE = energy ( path , 100) ! find new energy
! Metropolis algorithm
If ((newE > oldE) .AND. (exp( - newE + oldE ) < ranDom ())) then
path (element) = path (element) - change
EndIf
! add up probabilities
Do j = 1 , 100
element = path(j)*10 + 50
prop (element) = prop(element) + 1
End Do
oldE = newE
End Do
! write output data to file
Do j = 1 , 100
out = prop(j)
write (9 , *) j - 50 , out/max
End Do
close (9)
Stop ’data saved in qmc.dat’
End Program qmc
! Function calculates energy of the system
Function energy ( array , max )
Implicit none
Integer :: i , max
Real*8 :: energy , array (max)
energy = 0
Do i = 1 , (max - 1)
energy = energy + (array(i+ 1) - array(i))**2 + array(i)**2
End Do
Return
End
This is an open source code for calculating Feynman integrals in MATLAB: http://arxiv.org/pdf/1205.6872v1.pdf which can be run on any ordinary CPU and much faster on a GPU.
Since it only uses extremely efficient built-in MATLAB functions which are compiled to machine code, it's not expected to be significantly slower than FORTRAN or C (keeping in mind that the computational cost of calculating Feynman integrals scales exponentially with respect to the number of time steps, meaning that FORTRAN, C, and MATLAB will all be slow in many cases, and the differences between them will be much smaller than the difference between taking 12 time steps and 13 time steps).
If you run this MATLAB code on a GPU it will in fact be faster than the FORTRAN or C implementation (only a CUDA FORTRAN or CUDA C code will be able to compare).
If you have more questions about this code you can email the author at dattani.nike#gmail.com