Skip to main content

Questions tagged [data-structures]

Questions on using or implementing more or less common data structures such as linked lists, structs, trees, tries, heaps etc. in Mathematica.

Filter by
Sorted by
Tagged with
160 votes
3 answers
13k views

How to make use of Associations?

I have been curious about it for long. Now that Mathematica 10 arrives, I think it's time to ask the question: How the new Association data structure would be used ...
Yi Wang's user avatar
  • 7,347
123 votes
7 answers
18k views

Struct equivalent in Mathematica?

I really miss having something like a struct in Mathematica. I know of (and regularly use) a couple of programming techniques which feel like a ...
Cassini's user avatar
  • 5,566
122 votes
3 answers
9k views

What is a Mathematica packed array?

A simple sounding question with a few sub questions: What is the difference between unpacked vs packed array? Are packed arrays more space efficent, how much so? Are packed arrays more time ...
nixeagle's user avatar
  • 2,263
121 votes
1 answer
8k views

File-backed lists/variables for handling large data

Background Currently I am working with some large data (most of it generated by Mathematica itself). I usually find it a hassle to do this. For example, I just exported a large amount of data to ...
Szabolcs's user avatar
  • 235k
79 votes
7 answers
15k views

How can I add a column into a existing Dataset?

I'm very excited about the brand new Dataset function. I have played with it in Wolfram Cloud, and haven't figured out how can I add a new column into an existing <...
Murta's user avatar
  • 26.3k
65 votes
2 answers
11k views

How can I use Mathematica's graph functions to cheat at Boggle?

Boggle is a word game played with 16 dice and a 4x4 tray. This question is inspired by a Stack Overflow question about Boggle that I decided to solve using Mathematica. In addition to Mathematica, I ...
Pillsy's user avatar
  • 18.5k
48 votes
2 answers
3k views

Dataset vs an association of associations

Associations with a record structure (e.g., a flat table) can be organised in one of two main ways. First as a list of associations, which is also used in the case of Mathematica's data set ...
Mac's user avatar
  • 931
41 votes
5 answers
2k views

Error changing Dataset using Part

Update It was a bug in the documentation of V10.0, this functionality was not implemented yet, and V10.1 changed the documentation, removing it. It's a pity, because it's a very useful operations, ...
Murta's user avatar
  • 26.3k
39 votes
4 answers
4k views

How to create strong types?

In a program I'm writing, I create a list of lists, that looks something like this: {{100, {1,2,3,4,5}}, {105, {2,4,6,8}}, {42, {42,39,56}}} I then pass this ...
shanusmagnus's user avatar
  • 1,159
38 votes
1 answer
1k views

How to use the iterator in Mathematica

I find that Mathematica has the iterator data structure. We can build some iterators like this: Constant iterator GeneralUtilities`ConstantIterator[5] Key value ...
yode's user avatar
  • 26.8k
36 votes
10 answers
8k views

Visualize Pascal's triangle and other triangle shaped lists

How can Pascal's triangle be visualised like this in Mathematica? Or more generally, how can a 'triangular' list like {{1},{1, 1}, {1, 2, 1}} be visualized in ...
sjdh's user avatar
  • 7,757
36 votes
3 answers
2k views

Can a Trie be implemented efficiently?

What is the most space and time efficient way to implement a Trie in Mathematica? Will it be practically faster than what is natively available in appropriate cases?
Mr.Wizard's user avatar
  • 272k
34 votes
1 answer
691 views

Make my data-structure atomic

Lately, and by lately I mean since version 7 or so, the number of atomic expressions in Mathematica constantly grew. In former times only the native types like integers and optimised arrays were ...
halirutan's user avatar
  • 113k
28 votes
3 answers
2k views

Efficient priority queues?

I'm trying to figure out how to transfer the concept of a priority queue to the functional world. Searches have turned up some implementations that use Append and other expense list copying techniques....
Andrew White's user avatar
25 votes
4 answers
822 views

How to get complete Documentation Center graph of guide pages?

On the very last image below you can see a typical path of walking through Documentation Center guide pages. What is the best way to get the graph data and visualize the whole structure of these ...
Vitaliy Kaurov's user avatar
25 votes
4 answers
2k views

Efficient circular buffer?

I wish to create an efficient circular buffer. That is, I wish to keep a fixed length list while appending a new value and dropping the oldest, repeatedly. As we know lists in Mathematica are ...
Mr.Wizard's user avatar
  • 272k
24 votes
3 answers
2k views

Converting hierarchies of rules to associations

The impossibility of MatchQ-ing inside and Association MatchQ-ing Associations (MMA 10) explains a problem I have. I would like ...
John Jowett's user avatar
24 votes
5 answers
990 views

How can a big table be treated as a database?

I often work with big tables that I want to treat kind of like a database. Here's an example table. ...
ArgentoSapiens's user avatar
24 votes
3 answers
1k views

Emulating R data frame getters with UpValues

What's the best way to emulate R's data frames functionality? This includes the ability to select rows and columns in a 2-dimensional table by the string identifiers positioned typically in the first ...
alancalvitti's user avatar
  • 15.1k
23 votes
1 answer
497 views

Why do lookup times in Dataset and Association differ by orders of magnitude?

Consider the following three data structures: a list of rules, an association, and a dataset: ...
verse's user avatar
  • 1,287
22 votes
2 answers
2k views

How can one define a custom data object?

In Mathematica there are different objects like InterpolatingFunction or SparseArray? How can I define a custom data object with ...
PlatoManiac's user avatar
  • 14.8k
21 votes
1 answer
815 views

Mouse motion heat map - live visualization and analysis

Mouse motion heat map is an great way to study spatial attention distribution, styles of movement, reaction, etc. I am trying to design a code that visualizes such heat map. The requirements: 1) It ...
Vitaliy Kaurov's user avatar
20 votes
3 answers
11k views

Implementing a dictionary data structure

As one learns from a course on data structures, hash maps or dictionaries can be efficient when applied to appropriate tasks. I need a hash map in Mathematica and I've never found it. I'm scratching ...
Jonathan Prieto-Cubides's user avatar
20 votes
3 answers
852 views

How to implement a discrete data representation?

I'm currently working with large datasets of "discrete data". Each of these is a multiset of real numbers of the form $kS + B$, where $k$ is an integer, and $S$ (the "scale") and $B$ (the "bias") are ...
kjo's user avatar
  • 11.7k
20 votes
1 answer
760 views

Why won't SparseArray let me store values with the head List?

When you attempt to define a SparseArray with elements that have the head List it complains: ...
Mr.Wizard's user avatar
  • 272k
20 votes
1 answer
1k views

Learning Resources for Data Science in the Wolfram Language

I'm delivering an upcoming course on Data Science with the programming/analysis done predominantly in the Wolfram Language. I created the following table to leverage students' familiarity with Lists ...
Ronald Monson's user avatar
19 votes
3 answers
692 views

Difference between HashTable datastructure and Association datastructure

Is there a difference in the runtimes between CreateDataStructure["HashTable"] and <||> in terms of inserting, ...
Lucas Mumbo's user avatar
18 votes
2 answers
394 views

How to determine if two mutable data structures are the same?

How can I determine if two mutable data structures (DataStructure) are truly the same? I do not mean checking if they have the same value. I mean checking if they ...
Szabolcs's user avatar
  • 235k
18 votes
1 answer
988 views

Efficient code for tallying entries in very large lists

I am dealing with data sets containing tens of millions of (hashable) entries and simply using the Tally function to count the frequency of each unique list element ...
hailekofi's user avatar
  • 357
17 votes
2 answers
540 views

How to use DataStructure in FunctionCompile?

The official document said compile a function that can work with data structures but how to use it? There is no doubt that it is possible to use DataStructure as a function parameter: ...
miRoox's user avatar
  • 323
17 votes
1 answer
1k views

Creating a R DataFrame like construct in Mathematica

I kept getting my data mixed up so I decided to see if I could construct a package adding a R DataFrame like construct to Mathematica. I managed to cobble something together that sort of works, but I'...
Mr Alpha's user avatar
  • 3,156
16 votes
2 answers
458 views

Is there a method to enumerate the keys/values of System`Utilities`HashTable

Lacking from the simple API of System`Utilities`HashTable is a method to enumerate the keys/values. ...
Shuchang Zhou's user avatar
15 votes
1 answer
311 views

Substitute for Association inside Compile?

Associations are not compilable, so I’m looking for a workaround to this. That is, I need a mutable hashtable-like data structure with fast key value lookups ...
M.R.'s user avatar
  • 31.5k
15 votes
1 answer
1k views

How to use classical data structure in Mathematica

As a C programmer, I have used Mathematica for a month. In this process, I discovered that some problems cannot be solved via the built-ins, owing to lacking of data structure like ...
mma's user avatar
  • 317
14 votes
9 answers
1k views

Extract left-upper triangular data from a square matrix-form list

Hopefully, this question is not too basic or obscure. I'd like an idea on how to extract this {{1}, {5, 2}, {9, 6, 3}, {13, 10, 7, 4}} from ...
Oleg Melnikov's user avatar
14 votes
3 answers
681 views

Using Memoization with a Mutable Object

In looking for a solution to this question, I ran across some old binary tree code by Daniel Lichtblau, reproduced below: ...
rcollyer's user avatar
  • 34k
14 votes
2 answers
762 views

COVID-19 Wolfram Data Repository Retrieval

I'm using this dataset to check out the data on new COVID-19 cases. But there are two issues: Two days ago it was working great and it had data up to March 13; 1 day lag. Now when I run the cell in ...
Diffycue's user avatar
  • 1,834
14 votes
1 answer
274 views

Data re-structuring by Thread

Thread can do this Thread[{a, {b, c, d}}] {{a, b}, {a, c}, {a, d}} But when above ...
Αλέξανδρος Ζεγγ's user avatar
14 votes
1 answer
982 views

Pascal records and Mathematica programming

Background: "experienced" Pascal-programmer (DOS-era, never got into OOP) proud owner of Mathematica ver 11.2 have used Mathematica for years to do my share of number crunching and plotting to ...
Jyrki Lahtonen's user avatar
14 votes
2 answers
1k views

Question about designing a particular data structure

Background: I store polygons with a color assigned at the face level. In some occasions I want to use vertex colors but I want to store data efficiently, i.e. don't want to duplicate data to the ...
nilo de roock's user avatar
13 votes
3 answers
5k views

Excel Headers --> Mathematica

I have a large Excel .xls file. Miniature version shown below: (formatting should appear as two columns...) ...
nkormanik's user avatar
  • 309
13 votes
6 answers
282 views

How to invert MapIndexed on a ragged structure? How to construct a tree from rules?

I have an arbitrary ragged nested list-of-lists (a tree) like A = {{a, b}, {c, d}, {{{e, f, g, h, i}, {j, k, l}}, m}, n}; Its structure is given by the rules <...
Roman's user avatar
  • 47.5k
13 votes
4 answers
477 views

Short syntax for accessing System`Utilities`HashTableAdd and System`Utilities`HashTableGet

I have a need for a struct analogue in Mathematica. After reading this post I came to the conclusion that System`Utilities`HashTable is one of the best options. But ...
Ajasja's user avatar
  • 13.7k
12 votes
2 answers
400 views

Using DataStructures in FunctionCompile

This is a follow up to this StackExchange question I am trying to do the equivalent of Select on a "FixedArray" data structure. Here is what I have tried so far: ...
Craig Carter's user avatar
  • 4,425
12 votes
3 answers
217 views

Support for low-complexity (compressible) lists

By "low-complexity list" I mean a list like the following: lcl = {17, 17, 17, 17, 17, 21, 21, 21, 21, 21, 21, 21, 21, 11, 11, 11, 11, 11, 11, 11} ...
kjo's user avatar
  • 11.7k
11 votes
9 answers
223 views

List structure transformation

How to transform a list as {{x,y},{a,b,c,d,e}} into the form of {{x,y,a},{x,y,b},{x,y,c},{x,y,d},{x,y,e}} elegantly, ...
Αλέξανδρος Ζεγγ's user avatar
11 votes
1 answer
273 views

Implementing core data structures

Data structures are your friend. Using the right data structure in the right place means better performance and less work on the programmer's part. This poses a problem in Mathematica, of course, ...
b3m2a1's user avatar
  • 46.9k
11 votes
1 answer
535 views

How to use the new DataStructure in V 12.1 as is done in classical struct?

There is new DataStructure in V 12.1. I thought at first it is similar to classical struct or record found in other languages and discussed in Struct equivalent in Mathematica? How to use classical ...
Nasser's user avatar
  • 144k
10 votes
2 answers
532 views

Cases[data,Colon[key,_]] vs. Cases[data,key:_] toward XPath, XQuery

Exploring methods to index and search tree-structured key:value pairs via named-entity index (key paths) as opposed to Position-based indexing. Using Rule as ...
alancalvitti's user avatar
  • 15.1k
10 votes
1 answer
917 views

Tree-like data sets with efficient storage and retrieval

Most probably this is going to be a duplicate, sorry. I found too many questions/answers here about tree-like structures, and still could not locate what I want. It is a followup of my previous ...
მამუკა ჯიბლაძე's user avatar