Questions on the manipulation of List objects in Mathematica, and the functions used for these manipulations.
30
votes
6answers
4k views
Elegant operations on matrix rows and columns
Question
The Mathematica tutorial has a section 'Basic Matrix Operations', describing operations like transpose, inverse and determinant. These operations all work on entire matrices. I am missing a ...
14
votes
3answers
343 views
Using pure functions in Table
I need a table with the elements made of pure functions and list elements. This is a simplified example:
I need a list as:
...
1
vote
3answers
1k views
Delete duplicate elements from a list
If a list contains duplicate elements, for example
list = {a, 1, 5, 3, 5, x^2, x^2},
how can the duplicate elements be removed? The result would be
...
25
votes
4answers
650 views
Partitioning with varying partition size
How can I partition a list into partitions whose sizes vary?
The length of the $k$'th partition is a function $f(k)$.
For example: if $l = \{1, 2, 3, 4, 5, 6\}$ and $f(k) = k$. Then
the partitioning ...
23
votes
6answers
996 views
How to select minimal subsets?
I am a newbie, so please point me in the right direction if you feel this question has been answered somewhere else before. Here goes:
Suppose I have a list like this:
...
11
votes
5answers
721 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 ...
23
votes
2answers
464 views
Transpose uneven lists
Is there a quick method to transpose uneven lists without conditionals?
With:
Drop[Table[q, {10}], #] & /@ Range[10]
Thus the first list would have the ...
23
votes
12answers
812 views
Map a function across a list conditionally
It seems that this is a really basic question, and I feel that the answer should be obvious to me. However, I am not seeing. Can you please help me? Thanks.
Suppose I have a list of data ...
18
votes
6answers
811 views
11
votes
7answers
481 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:
...
4
votes
3answers
890 views
Local max/min of Mathematica data sets
Is there a way in Mathematica to find the local maxima of a set of points?
Suppose you have
...
13
votes
3answers
406 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 ...
8
votes
3answers
367 views
How do I use Map for a function with two arguments?
I'm a newbie who tries to be a good boy, and use Map instead of writing out a list of functions.
I have a table I want to Map ...
6
votes
2answers
414 views
Finding all partitions of a set
I'm looking for straightforward way to find all the partitions of a set.
IntegerPartitions seems to provide a useful start. But then things get a bit complicated.
...
29
votes
6answers
2k views
Does Mathematica have advanced indexing?
I have two $M \times K$ arrays $L, T$ where I would like to set all the elements in $L$ to zero whenever the corresponding element of $T$ is greater than 15. The ...
28
votes
2answers
590 views
Tiling a square
I wondered if there was a way to automate the process of finding a way to tile a tile into a square.
The idea is to represent the tile with a matrix of 0s for blank space and 1s for filled spaces ...
27
votes
6answers
1k views
How to visualize/edit a big matrix as a table?
Is it possible to visualize/edit a big matrix as a table ? I often end up exporting/copying big tables to Excel for seeing them, but I would prefer to stay in Mathematica and have a similar view as in ...
19
votes
5answers
954 views
What is the most efficient way to add rows and columns to a matrix?
Say I have a matrix m and a vector v.
...
26
votes
6answers
891 views
Find zero crossing in a list
I'm looking for a function that finds the index of the zero-crossing points of a list. Before I go making my own subroutine to do this, I was wondering if anyone knows of any built-in Mathematica ...
14
votes
6answers
847 views
Insert $+$, $-$, $\times$, $/$, $($, $)$ into $123456789$ to make it equal to $100$
Looks like a question for pupils, right?
In fact if the available math symbol is limited to $+$, $-$, $\times$, $/$ then it's easy to solve:
...
23
votes
7answers
975 views
How to apply or map a list of functions to a list of data?
Say I have a group of functions:
f1[a_] := a * -1;
f2[a_] := a * 100;
f3[a_] := a / 10.0;
and some data in a list:
...
17
votes
7answers
684 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:
...
12
votes
2answers
770 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 ...
19
votes
2answers
688 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 ...
15
votes
8answers
536 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 ...
13
votes
4answers
325 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 ...
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
...
9
votes
6answers
294 views
6
votes
5answers
248 views
Computing the equivalence classes of the symmetric transitive closure of a relation
I have a list of pairs, for example:
...
18
votes
3answers
545 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 ...
18
votes
11answers
639 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 ...
13
votes
5answers
957 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 ...
16
votes
7answers
809 views
How to Derive Tuples Without Replacement
Given a couple of lists like a={1,2,3,4,6} and b={2,3,4,6,9} I can use the built-in Mathematica symbol ...
12
votes
4answers
383 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:
...
10
votes
3answers
203 views
How to “ignore” an element of Map or MapIndexed
Say I have some function that I'm applying every element in a list to... if that element matches some criteria:
...
5
votes
2answers
505 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.
...
9
votes
6answers
397 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 ...
16
votes
8answers
422 views
Applying And to lists of Booleans
I'd like to take {True,True,False} and {True,False,False} and apply And to get ...
13
votes
7answers
604 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?
...
8
votes
4answers
361 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 ...
7
votes
5answers
747 views
How to find rows that have maximum value?
Suppose if I have following list
{
{10,b,30},
{100,a,40},
{1000,b,10},
{1000,b,70},
{100,b,20},
{10,b,70}
}
How to find rows that have max value in ...
6
votes
6answers
679 views
extract values from replacement list
Solve returns a list of replacement rules
In: Solve[x + y == 3 && x - y == 6, {x, y}]
Out: {{x -> 9/2, y -> -(3/2)}}
I am only interested in the ...
5
votes
1answer
177 views
DeleteCases messing with my mind
I'm losing my mind. Please tell me my laptop is doing weird things and not me. Or do I need to get committed someplace?
...
5
votes
2answers
207 views
From a list of dates get a list of the last date available in a each month
I wondered if anyone has another or even a more direct way of finding the last dates of each month available from a list of successive dates?
I currently do the following (note: nothing special about ...
2
votes
5answers
264 views
Assigning a particular value to array elements
I have an array of 10000 elements. I want to randomly assign energy to these 10000 elements using Gaussian or Exponential distribution, such that each time a particular element is selected its energy ...
1
vote
3answers
211 views
How to create functions of arbitrary number of variables?
In the following code what would be the simplest way to generalize it to say some $N_f$ number of $z$ instead of just $z_1$ and $z_2$?
...
13
votes
3answers
336 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
2k views
K-means clustering
In MATLAB, there is a command kmeans() that divides an array into $k$ clusters and calculates the centroid of each cluster.
Is there any command in Mathematica to ...
17
votes
3answers
343 views
How to efficiently find positions of duplicates?
Is there an efficient way to find the positions of the duplicates in a list?
I would like the positions grouped according to duplicated elements. For instance, given
...
14
votes
4answers
365 views
Implementing a function which generalizes the merging step in merge sort
One of the key steps in merge sort is the merging step. Given two sorted lists
sorted1={2,6,10,13,16,17,19};
sorted2={1,3,4,5,7,8,9,11,12,14,15,18,20};
of ...

