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

learn more… | top users | synonyms (3)

12
votes
2answers
1k 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
10answers
649 views

Elegant way to map a function to only the last element of each sublist [duplicate]

Suppose that I have the following list, called test: test = {{{a1}, {a2, a3}}, {{b1}, {b2, b3}}, {{c1}, {c2, c3}}}; Now ...
11
votes
11answers
448 views

Replace elements in list not matching elements of another list at the same position

I would like to compare two arrays: m = {a,b,c,d,d,f}; n = {b,b,a,d,d,e}; and get: {0,b,0,d,d,0} I have tried all kinds ...
11
votes
8answers
460 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
12answers
561 views

How to group each element with each consecutive elements in one head

Suppose I have the following list: list = {a, b, c, d} I want to generate this result: ...
11
votes
4answers
2k views

What is the cleanest way to prevent divide-by-zero warnings?

If I evaluate {1, 2, 3, 4}/{5, 6, 0, 8}, obviously I get one warning: ...
11
votes
7answers
2k 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
10answers
525 views

How to pick increasing numbers from the list

How to pick increasing numbers from the list. lst = {5, 3, 6, 2, 7, 4, 8}; out: {5,6,7,8} So many interesting ...
11
votes
3answers
1k 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 ...
11
votes
7answers
388 views

Concatenation of lists in a Fibonacci-like pattern

I am trying to create a List of elements that follow the general pattern: $$X_{n+1} = X_n X_{n-1}$$ where the operation on the right hand side is concatenation, i.e., joining. I want to achieve the ...
11
votes
7answers
217 views

DateList coupled with String Operations

Through some webscraping, I have the following list: ...
11
votes
5answers
859 views

How can I simulate this animation of checking for prime numbers?

I can only implement a very simple one, I want to make my code look like that. Any better ideas? ...
11
votes
4answers
383 views

What's fastest way of defining 10^5 down values?

I want to define isGood[___] = False; isGood[#] = True & /@ list where list is a list of several million integers. ...
11
votes
4answers
461 views

How to add lists with unequal length?

Given lists of unequal lengths, I want to add them together element-wise, treating missing elements as zero. For example, {1, 2, 3} + {a, b} + {x} should give ...
11
votes
5answers
2k views

Best way to create symmetric matrices

From time to time I need to generate symmetric matrices with relatively expensive cost of element evaluation. Most frequently these are Gram matrices where elements are $L_2$ dot products. Here are ...
11
votes
4answers
296 views

Thread a function over a list and with a non-atomic 2nd parameter?

From the documentation, Thread's behavior on functions where the first parameter is a List and the second is an atomic expression, is this: ...
11
votes
4answers
652 views

List comprehension in Mathematica

Python supports a concept called "list comprehensions". Following is sample example ...
11
votes
3answers
261 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
1k 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
177 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
536 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
458 views

How can I remove the singleton dimensions of an array? [duplicate]

If I have an array A, for which Dimensions[A] is {9, 9, 1, 1, 3, 4}, how can I convert ...
11
votes
1answer
188 views

How to find longest monotonic sequence?

I have a list, for example list = {2, 1, 3, 5, 4, 6} How to find longest ascending sequence of this list? There are two meanings of this question: Find the ...
11
votes
2answers
199 views

Bug with SparseArray selector in Pick

I have created a notebook with two cells. This is the content of the first: ...
11
votes
2answers
597 views

Vlookup function as Excel in Mathematica

How can we create a Vlookup function, similar to what we have in Excel? So if we have a table a and another b, we could add ...
11
votes
2answers
311 views

Solving the magic backyard puzzle with MMA

I was trying to solve this puzzle using mma It looks like the generalization of the magic square. Anyway here my attempt:- There are four columns $(x,y,z,w)$ and nine rowa which are denoted by the ...
10
votes
6answers
800 views

Multiplying two dimensional data by a constant

I have a list of lists of the form: {{1, 2}, {2, 4}, {2, 8}} But I want to multiply only the second dimension of that data by a constant. I know I could do this ...
10
votes
6answers
623 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 ...
10
votes
9answers
543 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
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. ...
10
votes
5answers
742 views

Building a continued fraction

I've completed a problem that involves approximating $e$ by a continued fraction: $$\frac{N_1}{D_1+\frac{N_2}{D_2+\frac{N_3}{\ddots+\frac{N_k}{D_k}}}}$$ with the $N_i$ being the list ...
10
votes
4answers
281 views

Searching linked lists that contain lists?

Following the advice I've read here and other sites, I've been trying to use the Mathematica equivalent of a linked lists... ...
10
votes
5answers
528 views

How do you efficiently return all of a List but one element?

I have a problem that I am working on that requires that I compare several lists of overlapping data points. As I traverse one of the lists, I'd like to only compare those data to the other lists, and ...
10
votes
10answers
665 views

Finding neighbors from list

Say you are given the following list: list={{3, 2, 1}, {3, 4, 2}, {2, 4, 1}, {1, 4, 3}}; Two numbers $x,y$, not necessarily distinct, are neighbors if one of the ...
10
votes
7answers
587 views

How can I Flatten this List with Inner List of Lists?

Given the following List ...
10
votes
5answers
498 views

How to find first list element that differs from average of N previous elements by more than a given amount?

Given a list of real numbers, I need to write a function that returns the value of the first element in the list that differs from the mean of the previous $N$ elements by greater than $P$ standard ...
10
votes
3answers
574 views

Fastest way to check for List membership

I have a set of say 100 numbers {1,3,7,11,19,...3971}. All elements are previously determined. I want to check whether ...
10
votes
3answers
645 views

Count consecutive positions with the same value

I want to program a function that counts the successive positions with the same value: In[]= ConsecutiveValues[{1, 1, 1, 1, 2, 2, 1, 0}] Out[]= {4, 2, 1, 1} In ...
10
votes
3answers
238 views

Using 0 to create new columns in a list with Part

I just discovered a nice way to create intermediary columns in a list, and I would like to know if I can have some problem with it. It Works like this: Imagine I have this list, 5x3: ...
10
votes
5answers
432 views

How would you efficiently implement BinListsBy?

Let's say I have a list like this: data = {{21, "Alice"}, {27, "Bob"}, {22, "Charles"}, {34, "Dora"}, {75, "Eliza"}} containing ages and names of persons. What I ...
10
votes
3answers
318 views

Looking for an elegant way to construct this tensor-product-ish list

I would like to make the following matrix: ...
10
votes
2answers
294 views

Using Gather to rearrange my data

I have a list of elements: {{1, 2}, {1, 5}, {2, 6}, {3, 7}, {4, 8}, {5, 6}, {5, 9}, {9, 13}, {10,11}, {11, 12}, {11, 15}, {15, 16}} I have to divide it into ...
10
votes
5answers
381 views

ListPlot under conditions

I have a data file containing several columns and thousands lines of data. Below I present a small portion of it, in order to express my issue: ...
10
votes
3answers
173 views

Pick with nested list

I have a list of pairs of values. Some of those pairs are invalid based on another list. I would like to select only the valid pairs. ...
10
votes
8answers
380 views

Forming an upper triangular matrix from the elements of a list

I asked this question before, but i was closed because it the question was not comprehensible enough. So i reworded it, hope that's ok. I have a list with 6 elements. ...
10
votes
4answers
325 views

How to pass a list of arguments into HoldAll

I have a list of arguments (which in reality is lengthy): arguments = {a, b, c} arguments2 = {a_, b_, c_} f[Sequence@@arguments2] := a + b + c Note: It seems ...
10
votes
3answers
309 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: ...
10
votes
2answers
337 views

Conditional Gathering of lists

Just need a little help with the GatherBy / SplitBy function(s). I have a list of random numbers here: ...
10
votes
3answers
535 views

What is the most efficient way to map a linked list?

In order to work with lists effectively using common recursive patterns from functional programming, it's often most convenient to worked with the "linked list" representation, that looks like this: ...
10
votes
4answers
715 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 ...