Import data from txt to Mathematica - import

Consider the following list in mathematica:
a = {
{
{0, 0, 0}, {1, 0, 0}, {1, 1, 0}
},
{
{0, 0, 1}, {1, 0, 1}, {1, 1, 1}
}
};
Now, invoke:
Export["test.dat", a]
and then
b = Import["test.dat"]
You will see that at the end a doesn't equal b. Should I consider this as a feature or a bug?
Furthermore, I would like to import a list having the following format: {P1,P2,P3...,Pn} where Pi={v1,v2,v3,...,vm} and each vi={x,y,z} where x,y,z are numbers representing the coordinates of the vertex vi. This should be a list of polygons.
How should I set my .dat file so I can read it with Mathematica, and how should I read it? I tried to imitate the output of Export["test.dat",a] above, but then I discovered the other issue. I found this question, but couldn't make the answer work for me...
Any ideas? Thanks in advance!

You should specify the exact format that you need to import/export in, otherwise Mathematica might not be able to guess the correct format.
So your question boils down to what textual format is suitable for storing 3D arrays?
If you work with Mathematica, probably the easiest thing to do is exporting the expression using Mathematica's expression syntax, i.e. Export["data.m", a, "Package"]. This format is relatively easy to write from other languages (but it's not as easy to parse). Your other option would be to make up some new easy to parse textual format for your 3D data, and write your own input/output functions for it in both Mathematica and the other languages you need to work with.
Since the format of the data you are working with is fixed (you always have coordinate triplets), the easiest solution may be to just flatten out your list before exporting, and partition it after importing, like this:
Export["test.txt", Join ### a, "Table"]
b = Import["text.txt", "Table"]
Partition[#, 3]& /# a

For storing MMA expression I'd suggest DumpSave (binary, system dependent),Save or Put, but if you want to use Export I'd convert a to a string , and export that as text. (I use ImportString and ExpertString below, so that I don't need a file, but it works the same for Import and Export). IMO this is solid as a rock.
a = {{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
b = ToExpression#ImportString[ExportString[a // ToString, "Text"], "Text"]
(* ==>
{{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}}
*)
a == b
(* ==> True *)
Reading your polygon list should work the same:
b = ToExpression#ImportString["test.dat", "Text"]

You may also do for example:
a={{{0,0,0},{1,0,0},{1,1,0}},{{0,0,1},{1,0,1},{1,1,1}}};
Export["c:\\test.dat",a,"MathML"];
b=ToExpression#Import["c:\\test.dat","MathML"]
(*
->{{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}}
*)
The additional benefit is that this method does not require parsing the Import output

I also go this Problem. My solution is the following:
IN[]: a = {{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
Export["test.dat", a, "List"];
b = ToExpression#Import["test.dat", "List"];
a == b
Out[]: True
Hope this helps. Best regards.

Related

Transfering algorithms AsymptoticOutputTracking to Matlab-Simulink

https://mathematica.stackexchange.com/questions/241776/transfering-algorithms-asymptoticoutputtracking-to-matlab-simulink
The following system is built and modeled in simulink.
I am interested in the transient process in the variable $x$.
This system is described by the following differential equations:
\begin{cases} \frac{dx}{dt}=-H(t) \ \frac{dH}{dt}+h H(t)=\frac{df}{dt} \end{cases}
where $f=e^{-(x(t)-x_e)^2}$,$x_e$=0,$h$=1
If we numerically solve this system in Mathematica, then we get:
Clear["Derivative"]
ClearAll["Global`*"]
pars = {xs = -1, xe = 0, h = 1}
f = Exp[-(x[t] - xe)^2]
sys = NDSolve[{x'[t] == -H[t], H'[t] + h H[t] == D[f, t], x[0] == xs,
H[0] == Exp[-(xs - xe)^2]}, {x, H}, {t, 0, 10}]
Plot[{Evaluate[x[t] /. sys]}, {t, 0, 10}, PlotRange -> Full,
PlotPoints -> 100]
We can see that the results from Simulink and Mathematica are the same.
Next, I want to do the following. Using the AsymptoticOutputTracker command, control the variable $x$ according to the specified reference law, i.e. the system of equations will take the form:
\begin{cases} \frac{dx}{dt}=-H(t)+u(t) \ \frac{dH}{dt}+h H(t)=\frac{df}{dt} \end{cases}
As a reference signal I use $r_1=e^{-t}+1$ with decay rate $p_1=-5$. As a controlled output, I use the variable $x$, and with $f$ I just see what happens in the end. There is my code:
(***)
Clear["Derivative"]
ClearAll["Global`*"]
f = Exp[-(x[t] - xe)^2]
asys = AffineStateSpaceModel[{x'[t] == -H[t] + u[t],
H'[t] + h H[t] == D[f, t]}, {{x[t], xs}, {H[t],
Exp[-(xs - xe)^2]}}, {u[t]}, {x[t], f}, t] // Simplify
pars1 = {Subscript[r, 1] -> Exp[-t] + 1, Subscript[p, 1] -> -5}
fb = AsymptoticOutputTracker[
asys, {Subscript[r, 1]}, {Subscript[p, 1]}] // Simplify
pars = {xs = -1, xe = 0, h = 1}
csys = SystemsModelStateFeedbackConnect[asys, fb] /. pars1 //
Simplify // Chop
plots = {OutputResponse[{csys}, {0, 0}, {t, 0, 10}]}
Plot[{plots[[1, 1]], Exp[-t] + 1}, {t, 0, 10}, PlotRange -> All,
PlotPoints -> 200]
Plot[{plots[[1, 2]]}, {t, 0, 10}, PlotRange -> All, PlotPoints -> 200]
In general, everything works, but now QUESTION:
And how to transfer the feedback signal generated by the AsymptoticOutputTracker command to simulink and in general simulate all this in simulink?
I would be grateful for the help and advice of respected experts.

PostGIS conditional aggregration - presence/absence matrix

I have a dataset that resembles the following:
site_id, species
1, spp1
2, spp1
2, spp2
2, spp3
3, spp2
3, spp3
4, spp1
4, spp2
I want to create a table like this:
site_id, spp1, spp2, spp3, spp4
1, 1, 0, 0, 0
2, 1, 1, 1, 0
3, 0, 1, 1, 0
4, 1, 1, 0, 0
This question was asked here, however the issue I face is that my list of species is significantly greater and so creating a massive query listing each species manually would take a significant amount of time. I would therefore like a solution that does not require this and could instead read from the existing species list.
In addition, when playing with that query, the count() function would keep adding so I would end up with values greater than 1 where multiples of the same species were present in a site_id. Ideally I want a binary 1 or 0 output.

Swift: BLE 16 bytes to Int

I'm getting a byte array like this one:
[60, 2, 0, 0, 0]
In the documentation there is written this:
uint16 -> heartBeatNum;
uint8 -> rawDataFilesNum;
uint8 -> alertNum
uint8 -> fallsNum
I will explain a little about the device so that you understand and then I ask my question.
The bluetooth device sends an object every minute that is called heartbeat. If this is the first time the object is to use the array looks like this:
After first minute:
[1, 0, 0, 0, 0]
After two minute:
[2, 0, 0, 0, 0]
After three minute:
[3, 0, 0, 0, 0]
After for minute:
[4, 0, 0, 0, 0]
...
Now there are more than 12 that have passed and the array is:
[60, 2, 0, 0, 0]
So I try to understand from the documentation the heartbeat count is the first 16 bytes. I can not figure out how to collect the 60's and the 2's to have the exact heartbeat number.
How does this function?
According to my calculation if I do 60 * 12 = 720
So I should have about 700
Can someone enlighten me how to gather the 16 bytes in int?

Distribute elements evenly (adding ints values to array of int values)

Say I have an array of Ints and all elements equal zero. It'd look something like this:
let arr: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
There are 11 elements in this array in total. I want three of the elements in this array to be the number one. I want these one values to be distributed evenly throughout the array so that it looks something like this:
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
I want to be able to add however many one's and distribute them evenly (or as close to evenly as possible) no matter how many total elements there are. How could I do this?
Note: For anyone wondering why I need this, I have a collection of strings that when joined together make up a large body of text. Think of the zeroes as the pieces of text and think of the ones as advertisements I am adding in between the text. I wanted to distribute these ads as evenly as possible. I figured this would be a simple way of expressing what I needed.
Maybe you can try this.
var arr: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
let distribution = arr.count / 3 // 3 is the number of 1s
for (index, value) in arr.enumerated() {
arr[index] = (index + 1) % distribution == 0 ? 1 : value
}
print(arr) // [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
Assuming that the value distribution > 1

Postive/Negative Chart in Google Visualization API

I need to generate a chart like this one:
Specifically, I want to show both a positive value and a negative value for a time period (could be an hour, minute, etc.) and display it like this.
I could have sworn I saw something like this on the Google Visualization API Gallery the other day, but I can't find it now, and am not even sure what this kind of chart is called.
First, do you know what this kind of chart is called so I can possibly find documentation? Second, is there any way to implement such a chart with the Google Visualization API? If not, is there another common charting solution for web that I can achieve this with?
Thank you for your time.
This is called a "Stacked Bar Chart", and can indeed be created with the Google Visualisation API.
Simply use the "isStacked" property (described here; http://code.google.com/apis/visualization/documentation/gallery/barchart.html).
Here's some sample code (based off the default bar chart example provided by Google and updated to show the use of isStacked and some sample data from your example);
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Month');
data.addColumn('number');
data.addColumn('number');
data.addRows(12);
data.setCell(0, 0, 'January');
data.setCell(1, 0, 'February');
data.setCell(2, 0, 'March');
data.setCell(3, 0, 'April');
data.setCell(4, 0, 'May');
data.setCell(5, 0, 'June');
data.setCell(6, 0, 'July');
data.setCell(7, 0, 'August');
data.setCell(8, 0, 'September');
data.setCell(9, 0, 'October');
data.setCell(10, 0, 'November');
data.setCell(11, 0, 'December');
data.setCell(0, 1, 19);
data.setCell(1, 1, 18);
data.setCell(2, 1, 20);
data.setCell(3, 1, 19);
data.setCell(4, 1, 18);
data.setCell(5, 1, 20);
data.setCell(6, 1, 19);
data.setCell(7, 1, 18);
data.setCell(8, 1, 20);
data.setCell(9, 1, 19);
data.setCell(10, 1, 18);
data.setCell(11, 1, 20);
data.setCell(0, 2, -12);
data.setCell(1, 2, -13);
data.setCell(2, 2, -11);
data.setCell(3, 2, -12);
data.setCell(4, 2, -13);
data.setCell(5, 2, -11);
data.setCell(6, 2, -12);
data.setCell(7, 2, -13);
data.setCell(8, 2, -11);
data.setCell(9, 2, -12);
data.setCell(10, 2, -13);
data.setCell(11, 2, -11);
data.setCell(0, 2, -12);
data.setCell(1, 2, -13);
data.setCell(2, 2, -11);
// Create and draw the visualization.
new google.visualization.ColumnChart(document.getElementById('visualization')).
draw(data,
{title:"S&P 500 Up/Down Performance Since 1980",
width:600, height:400,
isStacked:"true",
legend:"none" }
);
}
And the results...
Use ColumnChart instead of BarChart:
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
https://jsfiddle.net/0rrar9oq/16