How to iterate simultaneously two arrays and put the elements in a specific place in a function (in Mathematica)? - wolfram-mathematica

I want to make some pie plots and each one will have different data and different title.
columnofdata = {"Diagnóstico_de_demência", "Transtornos_psiquiátricos",
"Cadeirante", "Acamado", "Sequelas_neurológicas"};
titles = {"Diagnóstico de Demência", "Transtornos_psiquiátricos",
"Cadeirante", "Acamado", "Sequelas Neurológicas"};
PieChart[Counts[data[All, #1]],
PlotLabel -> Style[#2, 15]]
I want to get the first element of columnofdata array and put in the position where is #1, then the first element of the array titles and put it where is the #2.
Then get the second element of both arrays and so on.

Here is one way to do it
PieChart[Counts[data[All, First##]], PlotLabel -> Style[Last##, 15]] & /#
Transpose[{columnofdata, titles}]

Related

Why is sort function not sorting in Google Sheets

I have on row one cell A1 = 3, E1=9, I1=4.
If in cell K1 I write: =CONCATENATE(A1:I1) I get 394
But if I try to sort the cells before concatenating them with =CONCATENATE(SORT(A1:I1)) I still get the same result. Why?
This is due to the principle of the SORT function, which sorts rows according to data in columns. It does not sort individual cells. The example in the link explains more.
If you want to get the result that you anticipate, transpose your data and place them: A1=3, A5=9, A9=4
and perform: =CONCATENATE(SORT(A1:A9, 1, TRUE)), which gives you 349.
Or if you need to stick with row, transpose the cells in between. Use your original cell configuration and perform: =CONCATENATE(SORT(TRANSPOSE(A1:I1), 1, TRUE))
Note: Those additional parameters are required according to the SORT function help, however it seems that 1 and TRUE are used by default if they are missing.

Mathematica ListPlot two columns from the same table as (x,y) plot

I'm new to Mathematica, trying to ListPlot set of data having time in the first column and values in the columns 2 and 3.
t = Import["/.../time_deltas.csv", "CSV"]
{{11.1694,0.,0}, {11.1697,0.000275,0}, {11.1702,0.000495,0},
{11.1702,0.000028,0}, {11.1702,1.*10^-6,0}, {11.1702,0.000033,0},
{11.1707,0.000502,0}, {11.171,0.000314,0}, {11.171,4.*10^-6,0},
{11.1711,0.000025,0}, {25.8519,0.000029,1}, {25.852,0.000049,1},
{25.852,0.000032,1}, {25.8521,0.000031,1}, {25.8524,0.000388,1},
{25.8524,1.*10^-6,1}, {25.8525,0.000051,1}, {25.8543,0.001852,1},
{25.9342,0.079813,0},{25.9391,0.004914,0}}
Plotting a single point I expected that:
ListPlot[{t[[1,2,1]],t[[1,2,2]]}]
Would plot a single point with x=11.169662 and y=0.000275. Instead it plots two points with x=1,2 and y=11.169662, 0.000275.
What am I doing wrong ?
You need to group the data into x-y pairs for each list plot, e.g.
u = {{#1, #2}, {#1, #3}} & ### t;
v = Transpose[u];
GraphicsColumn[{ListPlot[v[[1]]], ListPlot[v[[2]]]}]
v[[1]] shows the data pairs you expect:
{{11.1694, 0.}, {11.1697, 0.000275}, {11.1702, 0.000495}, ... }

How to assign more than one value to UInt32

I am trying to set the bird group as two numbers so that when I assign a variable I can use multiple "else if" statements with that one group later on
Code:
Xcode doesn't let me do this I'm in Swift
Let birdgroup: UInt32 = 2, 3
You can use Array, Set, or a tuple to store multiple values in a single variable. If order matters, go with Array or tuple, but if the order doesn't matter, you can use Set. Array and Set both allow you to vary the number of values stored in your variable, while a tuple variable must always be the same length. Also, you can loop over the items in an array or set, but not over a tuple.
Array is the most often used of the three, so if you aren't sure which to use, it's a good first choice.
In summary, this table shows the possibilities and their properties:
Loopable Unloopable
Ordered Array Tuple
Unordered Set (none)
Finally, all the items in an array or set must be of the same type (or derived from the same type, if the array or set is defined with the base class). This is called homogeneous. A tuple can contain different types, also known as heterogeneous.
Homogeneous Heterogeneous
Ordered Array Tuple
Unordered Set (none)
Collection Types in the Swift documentation describes how to use Array and Set.
Array
Create an array with
var birdgroup: [UInt32] = [2, 3]
birdgroup[0] is equal to 2, and birdgroup[1] is equal to 3. You can also access the items by looping:
for bird in birdgroup {
println("\(bird)")
}
Set
You can declare a set with
var birdgroup: Set<UInt32> = [2, 3]
Because sets have no order (imagine every item is tossed together in a bag), you can't request the "first" or "second" item. Instead, loop over each item of the set:
for bird in birdgroup {
println("\(bird)")
}
Tuple
let birdgroup: (UInt32, UInt32) = (2, 3)
Tuples also retain the order of their items. birdgroup.0 is equal to 2, and birdgroup.1 to 3. You can also give each item of the tuple a name if you prefer that to a number:
let birdgroup: (UInt32, UInt32) = (foo: 2, bar: 3)
birdgroup.foo is 2, and birdgroup.bar is 3.
Additionally, the values in a tuple do not all need to be the same type. You can combine different types, such as
let heterogeneousTuple: (UInt32, String) = (2, "three")

how to print a dictionary sorted by a value of a subdictionary?

I have a dictionary inside a dictionary and I wish to print the whole dictionary but sorted around a value in the sub dictionary
Lesson = {Name:{'Rating':Rating, 'Desc':Desc, 'TimeLeftTask':Timeleft}}
or
Lesson = {'Math':{'Rating':11, 'Desc':'Exercises 14 and 19 page 157', 'TimeLeftTask':7}, 'English':{'Rating':23, 'Desc':'Exercise 5 page 204', 'TimeLeftTask':2}}
I want to print this dict for example but sorted by 'Rating' (high numbers at the top)
I have read this post but i don't fully understand it.
If you could keep it simple it would be great.
And yes i'm making a program to sort and deal with my homework
Thanks in advance
def sort_by_subdict(dictionary, subdict_key):
return sorted(dictionary.items(), key=lambda k_v: k_v[1][subdict_key])
Lesson = {'Math':{'Rating':11, 'Desc':'Exercises 14 and 19 page 157', 'TimeLeftTask':7}, 'English':{'Rating':23, 'Desc':'Exercise 5 page 204', 'TimeLeftTask':2}}
print(sort_by_subdict(Lesson, 'Rating'))
As there is no notion of order in dictionary, we need to represent the dictionary as a list of key, value pair tuples to preserve the sorted order.
The so question you mention sorts the dictionary using the sorted function such that it returns a list of (k, v) tuples (here k means key & v means value) of top level dictionary, sorting by the desired value of sub dictionary v.

Most efficient way to compile unique values in a massive text file?

I have a set of large text files that in total contain about 3 million rows.
What I want to do is pluck a value from a given column from each row and add it to an array in memory. If the value already exists in the array, then ignore it.
I'm assuming the fastest way is NOT:
Read a value
if exists (using array's native index or what-have-you method), then push it to the array
Should I be inserting the value in alphabetical order to speed up the match/search?
OR should I keep multiple arrays...for example, one for each letter of the alphabet?
Use Set:
Set implements a collection of unordered values with no duplicates. This is a hybrid of Array's intuitive inter-operation facilities and Hash's fast lookup.
Example usage:
require 'set'
set = Set.new
set << 1 << 2 << 3 # => #<Set: {1, 2, 3}>
set << 2 # => #<Set: {1, 2, 3}>
You could add the values as keys to a hash map, that would take care of removing duplicates automatically. You could even count the number of times each value occurs this way (with the hash value).

Resources