Questions tagged [list-manipulation]

Questions on the manipulation of List objects in Mathematica, and the functions used for these manipulations.

Filter by
Sorted by
Tagged with
29
votes
3answers
4k views

Extracting values from nested rules in JSON data

I have been using Mathematica to analyse some data from the StackExchange API. It is conveniently available in JSON form, which Mathematica interprets as replacement rules. However, some of the rules ...
28
votes
9answers
4k views

Find subsequences of consecutive integers inside a list

I have a list which is something like this: {3,4,5,6,7,10,11,12,15,16,17,19,20,21,22,23,24,42,43,44,45,46} What I'd like to to is get the intervals which are in ...
28
votes
6answers
2k views

Map-Thread-Through-Apply a list of functions onto a list of (lists of) values

I have a list of functions: fns = {f, g, h} and a list of triples: ...
27
votes
7answers
3k views

Efficient way to count the number of zeros at the (right) end of a very large number

If I want to count the number of zeros at the (right) end of a large number, like $12345!$, I can use something like: Length[Last[Split[IntegerDigits[12345!]]]] ...
27
votes
3answers
526 views

Bug in MovingMap (breaking change between version 10.1 and 10.0.2)?

The MovingMap window spec has been changed between version 10.1 and 10.0.2. Version 10.0.2 ...
27
votes
1answer
732 views

Undocumented form for Extract

Prompted by a comments conversation here, here's an interesting (and often performance enhancing) use of Extract: ...
26
votes
11answers
772 views

Using MapAt to map at a particular depth (levelspec)

I have the following sample list (my actual list is, of course, much longer): ...
26
votes
7answers
1k views

Elegant and fast way to join all string sequences

I want to do what title says: {"a","b",1,2,3,4,"a",2,2,2,"edg","?"} => {"ab",1,2,3,4,"a",2,2,2,"edg?"} in the most elegant way. For example one replacement rule for ...
26
votes
1answer
2k views

Two ways to map a function on a list: Which one is faster?

There are two ways to make a function listable on the list: Use Map or use SetAttributes. My question is, which one is faster, ...
26
votes
6answers
8k views

Best way to create symmetric matrices

From time to time I need to generate symmetric matrices with relatively expensive cost of element evaluation. Most frequently these are Gram matrices where elements are $L_2$ dot products. Here are ...
26
votes
2answers
3k views

Populating and storing a very large array in Mathematica

I am using Mathematica 8 to generate a very large multidimensional array of floating-point numbers and then store it in a file. Typical array sizes are 10000 x 50 x 15 x 40, resulting in a 3-4 GB ...
26
votes
1answer
1k views

Using Transpose with a list as the second argument

I'm having difficulty understanding the Transpose function. I know what the transpose of a matrix is, not a problem, and I see that applying the ...
25
votes
6answers
9k views

Best way to extract values from a list of rules?

Mathematica has a lot of list manipulation functions, and, also because I don't work with lists often, at times I'm a bit lost. I'll find a way, but I'm sure it's not the most efficient. Case in point,...
25
votes
7answers
925 views

Faster position list construction from a “take instructions” list

Take some list, e.g., {1,2,3,4,5}, and a "take instructions" list, say {1,1,2,1,1}. The latter is read as "take the first ...
25
votes
5answers
791 views

Partitioning a list when the cumulative sum exceeds 1

I have a long list of say 1 million Uniform(0,1) random numbers, such as: dat = {0.71, 0.685, 0.16, 0.82, 0.73, 0.44, 0.89, 0.02, 0.47, 0.65} I want to ...
25
votes
5answers
1k views

More efficient way of generating list of occurrence counts?

Given a list, e.g., target = {a, b, c, b, c, d, c, d, e, f, a, h, g}, I want to generate a list containing the occurrence count of the corresponding element of the ...
25
votes
2answers
5k views

Sorting a list with secondary criterion

Is there an easy way to sort a list on multiple levels of criteria? By this I mean, first the list should be sorted according by criteria A (the same as using the usual sort function, ...
25
votes
2answers
16k views

Is there an equivalent to MATLAB's linspace? [duplicate]

Does Mathematica have an equivalent to Matlab's linspace? I want to make a list with "start", "stop" and "number of points". What is the neatest way of doing this?
24
votes
6answers
3k views

Apply multiple functions to same list

I'd like to get the Min, Max, Median, Mean, etc. for the same list. For now I'm doing the following: ...
24
votes
15answers
3k views

How do I extract the middle element(s) of a given list?

How do I extract the middle element(s) of a given list? Here is some code that works, but seems a little too long: ...
24
votes
7answers
2k views

Equating matrices (or higher order tensors) element-wise

Say I have two matrices (or, as in my case, higher order tensors) $A,B$, and I want to solve the equation $A=B$. To do so , I need a list of equations that equate entry-wise the elements of $A$ and $B$...
24
votes
4answers
6k views

Data interpolation and ListContourPlot

I am fairly new to Mathematica and I have two quick questions on using it for a Hydrology and Hydrogeology class. One is about data interpolation and interpolating without any data defined in an area. ...
24
votes
3answers
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 ...
23
votes
8answers
2k views

Selectively Mapping over elements in a List

I am using the following code to easily generate a row of images of all eight planets of our Solar System: ...
23
votes
6answers
2k views

Is there a way to riffle more than two lists?

I thought Riffle would generalize such that Riffle[{a,b,c},{1,2,3},{x,y,z}] would return ...
23
votes
5answers
1k views

MapThread over sublists of different length

I have a list which looks like this: l={{1,0,3,4},{0,2},{0,0,1,3},{1,2,0}}. Now I would like to count how many 0s the sublists contain in the first, second,... slot....
23
votes
3answers
589 views

Is there a bug in Pick?

I wanted to use the Pick function with a condition. But there seems to be an issue here. Take a look at this: ...
23
votes
11answers
2k views

Generating an ordered list of pairs of elements from ordered lists

I have a pair of ordered lists. I want to generate a new ordered list (using the same ordering) of length n by applying a binary operator to pairs of elements, one from each list, along with the index ...
23
votes
6answers
1k views

How to split list into disjoint lists?

I wonder if there is any simple way to split a list into its disjoint sublists, e.g. from list={{a,b},{e,f},{b,c},{c,d},{f,g,h}} get a result ...
23
votes
2answers
764 views

Mysterious behaviour of Pick

As everyone knows, Pick can do amazing things, The most fascinating phenomenon is that it can pick though all sorts of lists: The basic & pattern matching: <...
23
votes
4answers
616 views

How can I regroup elements in a list into a tree based on their values?

I have a list of elements in an outline, here is an example that is only 3 levels deep: ...
23
votes
2answers
287 views

What's going on with performance of Tally here?

Observe: ...
23
votes
2answers
1k views

How can the {x,y,z} points that fall on the outer boundary of a set of values be selected and smoothly surfaced?

For a given set of x,y,z values, that may, or may not form a uniform shape, how can the center of the data cloud be found, and the surface points be located and a solid smooth surface created from ...
23
votes
0answers
1k views

Strip waste braces in nested list [duplicate]

I have nested lists wherein there are a lot of waste levels which looks like l = {{{{{{2, 2}}, 3}, 2, {{2, 33}}, 4, 5}}}; I would like to reduce it to somthing ...
22
votes
10answers
2k views

How to Map a subset of list elements to a function?

How would you, given a list {1, 2, 3, 4}, apply a function f to 1 and ...
22
votes
8answers
1k views

Applying And to lists of Booleans

I'd like to take {True,True,False} and {True,False,False} and apply And to get ...
22
votes
7answers
2k views

Permutations[Range[12]] produces an error instead of a list

This input: Permutations[Range[12]] Results in this (error) output: ...
22
votes
5answers
15k views

How to find the phase difference of two sampled sine waves?

I'm trying to measure a phase difference between two Sine functions I've acquired with a computer. I'm uploading one of the .txt files with the data I'm working with here: txt file. To remove the ...
22
votes
3answers
2k views

How to extract the edge from a set of points

I have an accumulation of points representing a body as you can see in the image below. From this data I want to generate a list of points or a curve, which is the envelope. The first idea was a ...
22
votes
4answers
5k views

Constructing a list that includes a leading zero (01,02,03 … 55, 56, etc.)

I am constructing a list for use in file operations. Say I want to create list which contains values from 01 to 87. The kicker is that the first nine integers need ...
22
votes
2answers
3k views

Finding a subsequence in a list

I have a list and I want to find (in this particular case the first) appearance of a any of some subsequences, of possible different lengths. None of the subsequences is a subsequence of each other. ...
22
votes
3answers
961 views

Typoglycemia text manipulation

I was just reading the Wikipedia page on Typoglycemia: ... readers can understand the meaning of words in a sentence even when the interior letters of each word are scrambled ... and wanted to ...
22
votes
1answer
7k views

How can I randomize order of elements in a list?

Is there a straightforward way to go from an ordered list like: ord = {a, b, c, d, e, f, g, h} to a disordered list like: ...
22
votes
1answer
508 views

Confused about how Partition works

I have a problem about the padding list about Partition command. For instance, ...
22
votes
1answer
603 views

Underlying Algorithms for List Manipulation Functions

Does anyone know, or know of a link for the underlying algorithms used for list manipulation functions? Specifically: Union with and without the ...
21
votes
14answers
3k views

Pick elements of largest absolute value

For example, given list = {{1, -3, -5}, {2, 1, 6}, {0, 2, 4}, {-9, 2, 6}} should return: {-5, 6, 4, -9} Updated I found ...
21
votes
9answers
3k views

How do you check if there are any equal arguments(even sublist) in a list?

I would like to set up a function which has to return True if at least two arguments of a given List are equal. So if I give {1,4,6,2} to the function it has to ...
21
votes
9answers
3k views

Selecting a sublist based on Length

If you have a simple list of lists as follows: test = {{1, 2}, {4, 5, 6, 7}, {5, 4, 3}} How do you ask Mathematica to return the sublist of greatest length? I'...
21
votes
12answers
4k views

How to get list of duplicates when using DeleteDuplicates?

This might be easy, but can't find a way to use DeleteDuplicates to get also list of the actual duplicates. Example: ...
21
votes
9answers
1k views

UnFlatten an array into a ragged matrix

Trying to unflatten an array that was part of ragged array of sub matrices. As input we have flattened array: ...