Questions on the manipulation of List objects in Mathematica, and the functions used for these manipulations.
13
votes
5answers
962 views
Finding all elements within a certain range in a sorted list
Suppose we have a sorted list of values. Let's use list = Sort@RandomReal[1, 1000000]; for this example.
I need a fast function ...
13
votes
5answers
445 views
How to thread a list
I have data in format
data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}}
Tableform:
I want to thread it to :
...
13
votes
6answers
846 views
Max of a table/list with indeterminate values
Suppose I have a Table:
tab = {1., 2., 3., Indeterminate}
When I type Max[tab] I get ...
13
votes
4answers
326 views
How can I remove B -> A from a list if A -> B is in the list?
I have a list of transformations like this:
list = {"A" -> "B", "B" -> "A", "C" -> "D"}
As this is used to plot an undirected graph with ...
13
votes
6answers
327 views
Matrix Rotation
If I have a 2 D matrix of any size say
$\left(
\begin{array}{ccc}
72 & 32 & 64 \\
18 & 8 & 16 \\
63 & 28 & 56 \\
\end{array}
\right)$
$\left(
\begin{array}{cc}
72 & ...
13
votes
7answers
318 views
Get the first element after the first sequence of length N of consecutively increasing values
I'm looking for an efficient way of extracting the first element after the first sequence of N consecutive elements in which the values are increasing.
If these are the data of a toy example:
...
13
votes
3answers
414 views
Efficient way to combine SparseArray objects?
I have several SparseArray objects, say sa11, sa12, sa21, sa22, which I would like to combine into the equivalent of {{sa11, sa12}, {sa21, sa22}}.
As an example, I ...
13
votes
3answers
338 views
Determining all possible traversals of a tree
I have a list:
B={423, {{53, {39, 65, 423}}, {66, {67, 81, 423}}, {424, {25, 40, 423}}}};
This list can be visualized as a tree using ...
13
votes
3answers
271 views
Quick multiple selections from a list
What is the fastest way to make multiple selections from a list? Compiled methods included.
For example, here are two methods for selecting a subset, compared:-
...
13
votes
5answers
281 views
Instruct a Table to only evaluate until a condition is fulfilled
I am looking for the way of building a Table of pairs of numbers in a fast way. My true table evaluates huge functions, and I see no way and reason to show those cumbersome expressions here. Let us ...
12
votes
9answers
413 views
Generating a matrix using sublists A and B n times
I want to write a function that generates a square matrix from sublists. My sublists are
a = Range[0, x, 0.5]; b = Range[0.25, x + 0.25, 0.5];
Suppose x=2, then I ...
12
votes
6answers
769 views
Find continuous sequences 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 ...
12
votes
4answers
385 views
Interlacing a single number into a long list
This seems like it should be a simple question, but I am running into some difficulty in doing this with Mathematica. Right now, I have a list like this:
...
12
votes
5answers
1k views
How to Delete Elements from List1 appearing in List2?
I'm new to functional programming of Mathematica and trying to remove one list of assorted elements from another. However I only find functions working with the Index rather than the values itself:
...
12
votes
9answers
333 views
Dropping n consecutive terms from a list periodically
Suppose I have the following list
lis = Range[100];
and I want to remove n consecutive terms periodically from the list. For example suppose I want to drop terms ...
12
votes
8answers
427 views
How to neatly get the sum of symmetric elements in a list?
The task is to compute
symmSum[{a, b, c, d, e, f}]
(*==> {a+f, b+e, c+d} *)
symmSum[{a, b, c, d, e}]
(*==> {a+e, b+d, c} *)
My clumsy solution is
...
12
votes
7answers
380 views
How to use Union on list of lists without sorting them first?
If I do
ClearAll[a, d]
lsts = {{a, d}, {a, d}};
Union[lsts]
I get the expected answer
{{a, d}}
but if I do
...
12
votes
6answers
558 views
Splitting up delimited data in lists
One task that I frequently find myself doing in Mathematica is splitting lists into lists of sublists, using specific elements to define the break-points. This is particularly useful with imported ...
12
votes
7answers
287 views
How do I obtain an intersection of two or more list of lists conditioned on the first element of each sub-list?
Given two lists like
list1 = {{1, 1}, {2, 4}, {3, 9}, {4, 16}};
list2 = {{2, 6}, {3, 9}, {4, 12}, {5, 15}};
I would like to produce an output like
...
12
votes
3answers
239 views
Accessing list elements by name
First, a bit of a long introduction to my problem:
I only have a few weeks of Mathematica experience. I am creating a mathematica application that calculates some material properties of steel based ...
12
votes
4answers
318 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 ...
12
votes
6answers
2k views
How to find the position of elements in a list satisfying criteria
Say I have a list x={2,4,6,8,10} and I want to find out the positions of the elements that are greater than 7.
...
12
votes
5answers
198 views
Faster way to convert triangle list to edge list with no duplicates
I have a list of triangles returned by a Delaunay triangulator, in the following format:
...
12
votes
4answers
234 views
The gap between MapAt and ReplacePart for 2D data tables with headers
Consider a relational table derived from survey data, where each column ("001-01" ...) represents a responder and each row (...
12
votes
4answers
370 views
Calculate proportion of values for dates
At work, we were discussing when is it the best time to change to winter tires for bikes and/or cars.
Using WeatherData[] and ...
12
votes
2answers
322 views
12
votes
4answers
639 views
What is the right way to rotate an array?
Suppose I have an array, not necessarily square:
a = $\left(
\begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}
\right)$
I want to rotate it like one ...
12
votes
2answers
432 views
How do I split up a curve into segments of equal length?
I have a curve that is defined as f[x] and what I'm attempting to do is to divide the curve into equal straight lengths for a number of segments of my choosing that I've defined as nSeg.
I've created ...
12
votes
1answer
256 views
How to extract and replace submatrix of a doubly-periodic matrix?
How can I effectively extract and replace a rectangular subpart of a toroidal matrix, i.e. one where boundaries are connected at opposite ends? Since the submatrix can overhang the edges, a simple ...
12
votes
1answer
129 views
How can I make threading more flexible?
Threading automatically with Listable functions requires the argument expressions to have the same length (or for one of them to be atomic). For nested lists the ...
12
votes
1answer
79 views
Why changing list's head takes time?
Consider
AbsoluteTiming[Range[10^7];][[1]]
0.035000
and
AbsoluteTiming[HoldComplete @@ Range[10^7];][[1]]
...
12
votes
2answers
773 views
Simple algorithm to find cycles in edge list
I have the edge list of an undirected graph which consists of disjoint "cycles" only. Example:
{{1, 2}, {2, 3}, {3, 4}, {4, 1}, {5, 6}, {6, 7}, {7, 5}}
Each ...
11
votes
8answers
380 views
Any built-in function to generate successive sublists from a list?
Given
lst = {a, b, c, d}
I'd like to generate
{{a}, {a, b}, {a, b, c}, {a, b, c, d}}
but using built-in functions only, ...
11
votes
5answers
520 views
From a list to a list of rules
Starting from these two lists,
var = {a, b, c}
values = {{1, 2, 3}, {4, 5, 6}, {7, 8 , 9}}
how can I generate a list of rules?
...
11
votes
7answers
356 views
DeleteCases on list of Strings
Consider the following:
data={"AB","CD","AF"};
Now I would like to delete all String from data which starts with "A".
...
11
votes
7answers
1k views
Combination and Permutation
How could I obtain the list of all the groups of 5 numbers taken from Range[12] such that the 2 lists have an empty intersection :
...
11
votes
7answers
396 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 ...
11
votes
7answers
486 views
How to Set parts of indexed lists?
I would like to assign a list to an indexed variable and then change it using Part and Set like this:
...
11
votes
3answers
265 views
Optimising 2D binning code
I have a set of (x,y,z) data, 45,000 to be precise and I want to bin the z values in 256 equidistant bins based on their (x,y) values. The final array should be a set of 256x256 array with each slot ...
11
votes
5answers
730 views
Partition a set into subsets of size $k$
Given a set $\{a_1,a_2,\dots,a_{lk}\}$ and a positive integer $l$, how can I find all the partitions which includes subsets of size $l$ in Mathematica? For instance, given ...
11
votes
2answers
357 views
Are there advantages to using generalized Part extraction instead of specialized functions like First, Last?
There are many ways to extract different parts of lists in Mathematica. For example, the first part of a list v can be accessed either as ...
11
votes
3answers
213 views
Threading over a list of lists of lists and a list of atomic expressions
Although I solved the following problem using MapThread, I'm curious about why Thread does not work as I expect it to here.
...
11
votes
2answers
321 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, ...
11
votes
3answers
277 views
Delete duplicates in a list, depending on the sequence of numbers
Below, list is a representative sample of my list, which contains lists of integers. I would like to be able to input:
...
11
votes
3answers
144 views
Why can't I use Sequence to perform a Select like task?
Suppose I wanted to write down a list of $p^2$, for $p$ a prime between $1$ and $20$. I would expect
Table[If[PrimeQ[k], k^2, Sequence[]], {k, 1, 20}]
to work. ...
11
votes
3answers
760 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. ...
10
votes
6answers
559 views
How do I check if any element in a list is positive?
As a simple example of what I would like to do, suppose I have a list a of all real numbers. I would like to perform a simple check to see if some element of ...
10
votes
9answers
404 views
How to find range in which a number falls, from given list of numbers?
How can one find the range in which a number falls, from given list of numbers?
...
10
votes
3answers
398 views
What is Mathematica's equivalent to MATLAB's filter function?
The MATLAB code
filter(0.5,[1, -0.5], [1:10])
is equivalent to
Rest@FoldList[(#1 + #2)/2. &, 0, Range[10]]
I don't ...
10
votes
7answers
176 views


