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

learn more… | top users | synonyms (3)

58
votes
7answers
12k 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 ...
34
votes
5answers
2k 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 ...
17
votes
3answers
763 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: ...
23
votes
6answers
1k views

Mathematica Destructuring

Context I'm writing a function that look something like: ...
27
votes
5answers
2k 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 ...
11
votes
6answers
376 views

List-operations only when restrictions are fulfilled (Part 1)

Consider the following: ...
20
votes
5answers
1k 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 ...
24
votes
5answers
3k 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. ...
4
votes
4answers
2k 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 ...
27
votes
12answers
1k 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 ...
26
votes
4answers
595 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 ...
20
votes
1answer
418 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 ...
17
votes
3answers
626 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 ...
9
votes
5answers
2k 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 ...
9
votes
6answers
2k 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 ...
40
votes
6answers
3k 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 ...
31
votes
6answers
2k 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 ...
26
votes
3answers
2k views

Data Table Manipulation in Mathematica

I am a statistician searching for an efficient way to select rows or columns from a table of data in Mathematica. Let me pose the question in 2 parts with a SQL-style table of data: ...
24
votes
6answers
1k 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: ...
18
votes
7answers
530 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 ...
15
votes
7answers
1k 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
3answers
2k 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 ...
2
votes
2answers
228 views

Position function not always retuning an answer even with no apparent problems

I'm having some problems with Position. Sometimes it will give an empty list instead of the actual position of the element I am looking for when that element is ...
21
votes
4answers
1k 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 changes to the other lists so that I ...
26
votes
2answers
855 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 ...
13
votes
9answers
558 views

Thread over list in different levels

I have these two lists, both with the same length in the first dimension: ...
5
votes
4answers
456 views

Combining the second elements of a set of ordered pairs based on their first element [duplicate]

I have a large set of ordered pairs and I am trying to add the second elements together for any group of pairs that has the same first element. For example my data looks like: ...
34
votes
6answers
2k 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 ...
15
votes
6answers
1k 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: ...
16
votes
5answers
2k 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
6answers
525 views

Sort+Union on a list

Probably this is too trivial question for this forum but for some reason I am not getting it on my Mathematica 7 so far. I have a list ...
16
votes
9answers
981 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 ...
16
votes
3answers
724 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
5answers
529 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: ...
17
votes
8answers
553 views

Applying And to lists of Booleans

I'd like to take {True,True,False} and {True,False,False} and apply And to get ...
2
votes
1answer
523 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 ...
15
votes
7answers
705 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}
28
votes
7answers
2k 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: ...
21
votes
3answers
793 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 ...
17
votes
5answers
694 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 ...
13
votes
11answers
792 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: ...
17
votes
5answers
610 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: ...
14
votes
1answer
205 views

Ordering function with recognition of duplicates

Fairly often I have a need to get the Ordering of an expression but with recognition of duplicates. For example: ...
12
votes
5answers
2k 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 ...
5
votes
5answers
926 views

Replace values which obey certain criteria

I'd like to replace all values in a list which obey one or more criteria with another value. Example: Replace all values>30 by 30. data={{3,35},{2,7}} ...
14
votes
4answers
447 views

“Unflattening” a list

Imagine we have a list with some hierarchical structure: list = {{{2,3},{2,4}},{{{3,4}}},{{5,6},{7,8}}}; Which we then flatten to some level in order to process ...
19
votes
2answers
1k 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 ...
10
votes
8answers
2k views

Map and Apply a function on a nested list

I have a list like this: {{1,2}, {4,2}, {6,4} ... } I want to replace every second number with a function of that number. For e.g. ...
7
votes
3answers
486 views

how to efficiently apply function to all pairs of a set (and collect the results)

To build a graph, I need to apply a function f[a_, b_] to all pairs of a list (3500 elements). The function itself returns a link ...
13
votes
4answers
373 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 ...