Questions tagged [list-manipulation]
Questions on the manipulation of List objects in Mathematica, and the functions used for these manipulations.
9,397
questions
14
votes
6
answers
2k
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 :
...
4
votes
1
answer
277
views
3x+1 on arithmetic chains, tree pruning and NestList
Given the following function Collatz:
...
7
votes
3
answers
847
views
How to create a list of pre-defined functions?
I would like to create a list of Cauchy distribution pdf's, having different locations of their maxima, and being multiplied by different factors; then I'd like to calculate those functions' values ...
9
votes
2
answers
609
views
45
votes
6
answers
3k
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 ...
44
votes
8
answers
9k
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:
...
11
votes
2
answers
1k
views
Handling vector comparisons in "Piecewise" function
Is it possible for the Piecewise function to process a vector comparison? For example, my input vector is
x={0,1,2,3,4}
I would like to define a function that ...
3
votes
1
answer
181
views
Issues with a Counter that is tallying term appearances
I am working with an array of textfiles, trying to see how frequent certain terms are. However, I am running into some difficulty with my running counts of bigram frequency. Here is a snippet of my ...
35
votes
2
answers
3k
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 ...
11
votes
1
answer
2k
views
Modifying a List in a function in place [duplicate]
An example will be most specific:
func[list_, column_] := list[[All, column]] = Map[#*2 &, list[[All, column]]];
This throws errors.
I want to avoid doing ...
33
votes
9
answers
1k
views
Sequentially numbering a nested list
I've got a nested list
{a, b, {c, d}, e, {f, {g, h}}}
which I want to magically transmogrify to
...
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 ...
4
votes
2
answers
378
views
Why can't NumberQ be used as the head of a pattern?
Consider the following:
list={1/First[{}], 1, 2, 1/First[{}], 3};
DeleteCases[list,_NumberQ]
I wanted to remove all cases, which did not match ...
14
votes
4
answers
1k
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:
...
16
votes
2
answers
668
views
Conditional Gathering of lists
Just need a little help with the GatherBy / SplitBy function(s).
I have a list of random numbers here:
...
17
votes
4
answers
367
views
Setting parts of a list
Suppose I have list
a = Range[10]
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
in which I want to set some elements to be a list
...
2
votes
2
answers
504
views
MapThread on a nested Map
A simple problem I am facing is here:
...
4
votes
1
answer
387
views
Path queries for tree-structured data
Can anyone suggest documentation or tutorials for developing path queries and indices for (XML-like) tree-structured data?
Suppose data is organized hierarchically in key->value pairs, eg:
...
-1
votes
1
answer
574
views
variable sized lists and using lists as variables
I am trying to scan a parameter space of varying numbers of parameters subject to some constraints (I am interested in any number of constraints just out of curiosity, but in reality no more than 2 ...
5
votes
1
answer
3k
views
How do I treat elements in a list as variables inside a module?
I want to be able to assign values to a list element inside a module. For example if I have:
...
12
votes
5
answers
439
views
Make a ragged list rectangular by trimming instead of padding
I have a ragged list
ragged = {{a,b,c,d,e},{x,y,z}}
that I would like to trim (on the right) to be rectangular. The desired result is
...
15
votes
2
answers
3k
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 ...
27
votes
7
answers
3k
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$...
19
votes
8
answers
7k
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 ...
27
votes
4
answers
7k
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 ...
19
votes
7
answers
17k
views
Mathematica: finding min/max in list
I have a list of coordinates like this one:
{{x1,y1},{x2,y2},{x3,y3},...,{xn,yn}}
I need to get the minimum and the maximum of all x-values and the minimum and ...
7
votes
2
answers
262
views
Operate on several lists to create one list
How do you put corresponding values from different lists together according to some operation?
E.g. I have three lists giving the velocity in the x, y and z direction respectively. I'd like create ...
29
votes
3
answers
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 ...
52
votes
4
answers
12k
views
Why does MatrixForm affect calculations?
This is a really newbie question, but it has me confused. Why does this code work without // MatrixForm and doesn't work with ...
35
votes
6
answers
8k
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?
...
201
votes
14
answers
74k
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 ...
42
votes
6
answers
4k
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
2
answers
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 ...
3
votes
2
answers
12k
views
Creating Lists From Loops
Consider the following:
lists = Import["G:/Notebooks/Lists.csv"];
col7 = lists [[All, 7]];
For[i=0,i<101,i++,Print[Count[col7,i]]]
This will get a 100 cells ...
11
votes
6
answers
513
views
Fill out blanks with a upcoming number in a list?
I have a bunch of arrays looking like this:
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "...
3
votes
1
answer
462
views
n-fold symbolic integral in Mathematica
I am trying to compute symbolically a n-fold integral (n is a parameter of a function) over, say, the cube [0,a]^n. My code looks like this
...
4
votes
1
answer
249
views
What's the most intelligent way to store the information of a selfmade graph?
I have a very long list of element, say $a1,a2, a3$,..., and they are all connected in that each is the child of many parents. E.g. $a74$ is associated with $a2,a55,a71$, and also contains information,...
6
votes
1
answer
655
views
Defining a string based sort function
I'm having a bit of trouble figuring out the right Sort syntax to use on a list of lists. Consider the following list:
...
75
votes
6
answers
7k
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 ...
11
votes
4
answers
13k
views
How can I multiply matrix and vector element wise like Numpy?
I have an $M \times K$ matrix $C$ and a length $K$ vector $v$. I want to form the $M \times K$ matrix $S$ where $s_{ij} = C_{ij}/v_j$. To do this in numpy I simply divide the two objects like this
<...
4
votes
1
answer
543
views
Deleting parts of a large list
At present I am running an analysis on economic data. Within the data I was able to identify countries which went through recession. I then calculated for example the average decline rate of GDP ...
20
votes
1
answer
569
views
Are there advantages to using additional arguments of common functions rather than alternative ways of calculating with lists?
(Apologies for the long question title.)
One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can ...
33
votes
4
answers
3k
views
Clustering of space-time data
Below is an example of a gaze sequence I recorded during a 3 seconds display. That is, where the eye was at every millisecond. While we should have 3000 points, we are missing some due to blinking.
...
10
votes
7
answers
813
views
How to efficiently Append a result of an operation on each element of a list to itself
I'm looking for the best function to apply the product of the last two elements of sublist elements to each element:
Example:
...
10
votes
4
answers
1k
views
Efficiently Visualising Very Large Data Sets (without running out of memory)
I have put a few really hard problems in combinatorics up against Mathematica 8. I'd have to say that it works really well, until you want to view the data. If you look at my question Advanced ...
18
votes
2
answers
813
views
Count number of sublists with a total not greater than a given max
Suppose I have a list of positive integers:
data={1, 1, 2, 3, 3, 3, 5, 5, 5, 7, 7, 8, 8, 9, 10, 10, 12, 16, 23}
I want to count the number of subsets up to ...
28
votes
5
answers
6k
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 ...
2
votes
1
answer
277
views
Conditional Data Selection, efficiency
Building up on the solution proposed here : Simplifying nested If statements
You can find here the data set : allGazes.dat
...
9
votes
5
answers
452
views
How to distinguish between lists and values?
I have a (hopefully small) problem with some numerical integration algorithm, more specifically I want to integrate the imaginary part of a complex valued function, e.g. ...
17
votes
5
answers
4k
views
Select/Delete with Sublist elements?
Probably easy and short question, I still didn't fully figure out how to easily select/delete sublists from a list. Example:
tt = {{2, 4}, {4, 8}};
I want to ...