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
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
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 ...
Alexey Kalmykov's user avatar
205 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 ...
sjdh's user avatar
  • 7,707
46 votes
4 answers
4k views

Function 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: ...
yulinlinyu's user avatar
  • 4,805
111 votes
4 answers
7k views

Flatten command: matrix as second argument

One thing I could never wrap my head around is how Flatten works when provided with a matrix as the second argument, and the Mathematica help isn't particularly ...
David's user avatar
  • 14.9k
78 votes
8 answers
7k 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 ...
sjdh's user avatar
  • 7,707
21 votes
7 answers
2k views

Combining values in one column (or part) when values in another column (or part) match

Consider the following: ...
John's user avatar
  • 4,361
77 votes
9 answers
12k 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 ...
Michael E2's user avatar
  • 234k
33 votes
10 answers
7k 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 ...
mgm's user avatar
  • 1,029
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 ...
Brian B's user avatar
  • 1,553
43 votes
6 answers
3k views

Mathematica Destructuring

Context I'm writing a function that look something like: ...
user avatar
45 votes
5 answers
24k 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. ...
Andy Ross's user avatar
  • 19.3k
39 votes
11 answers
6k views

Removing elements from a list which appear in another list

There are two lists {a, b, c, a, d, a, e} and {a, c, a}. I need to remove those elements from the first list which appears in a second list, to get {b, d, a, e}
Karus's user avatar
  • 391
62 votes
9 answers
5k views

Levels: how do they work?

I cannot understand how Mathematica manages levels, and so it's always a painful try-and-fail to use Flatten. Can someone please give me a very clear definition? ...
Lorenzo Pistone's user avatar
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 ...
Helium's user avatar
  • 4,049
40 votes
2 answers
1k 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 ...
Simon Woods's user avatar
  • 84.8k
49 votes
4 answers
32k views

How to remove redundant {} from a nested list of lists?

There are numerous examples here, whose end result is the removal of empty brackets {} and empty lists. I still can't find an example of simply removing redundant ...
mystackexchangeusername's user avatar
36 votes
6 answers
7k 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 ...
Szabolcs's user avatar
  • 234k
35 votes
3 answers
10k views

How do I convert an argument list to a sequence of arguments?

I think this is a basic question, but I am having difficulty finding the answer in the documentation. Thread is not what I am looking for, I think. Suppose that I ...
Andrew's user avatar
  • 10.5k
56 votes
3 answers
2k views

Are there guidelines for avoiding the unpacking of a packed array?

Packed arrays are very useful because they save memory and generally allow speedier and more efficient calculation times. If a list of data currently stored as a packed array is unpacked, it can slow ...
Verbeia's user avatar
  • 34.2k
48 votes
10 answers
17k 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: ...
PeriodicProgrammer's user avatar
40 votes
4 answers
12k views

Sort lists according to the order of another

I have three parallel lists (i.e. the elements in position i of each list are related). I want to sort the first list using the function Sort and make the same ...
Tyson Williams's user avatar
17 votes
7 answers
5k 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 ...
Prashant Bhate's user avatar
22 votes
8 answers
3k 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: ...
gwr's user avatar
  • 13.4k
37 votes
9 answers
4k 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: ...
MrDas's user avatar
  • 473
35 votes
10 answers
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: ...
Brett Champion's user avatar
12 votes
3 answers
2k views

How to represent a list as a cycle

I want to select distinct cycles (represented by List). The cycle {a, b, c, d} must be the same as {b, c, d, a} or ...
praaeew's user avatar
  • 143
45 votes
10 answers
6k 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 ...
dthor's user avatar
  • 971
34 votes
4 answers
1k views

How to make MapAt work with Span?

Span (;;) is very useful, but doesn't work with a lot of functions. Given the following input ...
M.R.'s user avatar
  • 31.4k
28 votes
4 answers
8k 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. ...
John Lombardi's user avatar
26 votes
13 answers
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 ...
Teo Sartori's user avatar
25 votes
7 answers
2k views

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

This input: Permutations[Range[12]] Results in this (error) output: ...
Harold's user avatar
  • 1,017
19 votes
3 answers
2k 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 ...
Mun's user avatar
  • 377
11 votes
6 answers
1k views

Prepend 0 to sublists

My question is similar to this one, but my goal is to prepend a single 0 the each sublist, not incrementally many 0's. The file I'm working is a CSV containing around 50K sublists of length 35. I've ...
CHM's user avatar
  • 3,395
39 votes
12 answers
3k 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 ...
Andrew's user avatar
  • 10.5k
19 votes
8 answers
1k 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 ...
Erik Tjernlund's user avatar
12 votes
4 answers
8k 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 ...
sjdh's user avatar
  • 7,707
6 votes
3 answers
370 views

How to 'merge' a list like FromDigits, but with a mixture of numbers and symbols?

How do I go from {C,4,G,5,S,7} to C4G5S7
Simon's user avatar
  • 493
23 votes
8 answers
2k views

Applying And to lists of Booleans

I'd like to take {True,True,False} and {True,False,False} and apply And to get ...
ArgentoSapiens's user avatar
20 votes
4 answers
6k views

How to use "Drop" function to drop matrix' rows and columns in an arbitrary way?

The built-in function "Drop" can delete a Matrix's row and column. Typical syntax for "Drop" is as follows: Drop[list,seq1,seq2...] But what if I want to drop a ...
matheorem's user avatar
  • 17.1k
20 votes
4 answers
2k views

Using Position (which requires a pattern) in the same manner as Select (which requires a criterion)

I am using Mathematica7. I have a list of lists (creatively named list in the following sample code) containing data. I would like to find the positions of the ...
Andrew's user avatar
  • 10.5k
16 votes
2 answers
6k 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. ...
DavidC's user avatar
  • 16.7k
16 votes
7 answers
3k views

A function that accepts a pair or a list of pairs

Probably a duplicate, but it's not easy to search on "lists". I have a function that accepts a list of lists, say {{a1, b1}, {a2, b2}, {a3, b3}} and performs some ...
rogerl's user avatar
  • 4,209
14 votes
6 answers
4k views

Prevent Part[] from trying to extract parts of symbolic expressions

If you have a list, e.g. {1, 2, 3} then you can extract the $k$th part using Part (...
Mechanical snail's user avatar
11 votes
3 answers
11k views

Using sets of arguments from a list

Please Consider : list={{5,2,6},{2,8,3}} I need to get the product of all the element within list : As of now I do this : ...
500's user avatar
  • 5,529
7 votes
2 answers
6k views

Contracting with Levi-Civita (totally antisymmetric) tensor

I have an array $v_{ijk}$ which is effectively a rank-$3$ tensor with dimensions $3\times3\times3$, and I need to contract it with $e^{ijk}$, i.e. evaluate $v_{ijk} e^{ijk}$ (see Einstein convention)....
S D N's user avatar
  • 71
36 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 ...
Mr. Demetrius Michael's user avatar
24 votes
2 answers
4k 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. ...
Rojo's user avatar
  • 42.6k
23 votes
4 answers
9k 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 ...
stevenvh's user avatar
  • 6,856
22 votes
9 answers
4k 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 ...
Gianpiero Cea's user avatar

1
2 3 4 5
32