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

learn more… | top users | synonyms (5)

11
votes
6answers
409 views

For a given expression: if it appears, remove it, but if it is absent, add it

While reformatting Szabolcs's code from (42660) I noticed this interesting operation: ...
11
votes
4answers
184 views

Faster position-based duplicate removal in a ragged array?

Take a ragged 2D array: rArray={{4, 2, 1, 0}, {2, 2}, {0, 0, 3, 3, 3}, {4, 0}, {3, 4}} I need to "deduplicate" this, where a duplicate is any element of a ...
11
votes
3answers
305 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
3answers
199 views

Why can't I use Sequence to perform a Select like task? [duplicate]

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
2answers
2k views

Estimating the full width half maximum of a peak

I have a dataset of which I'm attempting to determine the full width at half maximum. A picture of the data is given below, and the dataset itself can be found here (pastebin). Now, this set has a ...
11
votes
5answers
579 views

Find all the possible ways of partitioning a list into a set of pairs of element

I have a list {x1,...,xN} where N is even, and I need to find all the possible ways to split it into pairs of elements, e.g. the ...
11
votes
1answer
233 views

Map efficiently over duplicates in list

I am interested in mapping a function, f over a (long) list which contains duplicates. Since this function takes a while to evaluate I would like to make use of ...
11
votes
1answer
307 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
194 views

Behavior of Inner depends on List dimensions?

Consider the following case: ...
11
votes
1answer
102 views

Speed up Dynamic for graphical interface

I am trying to create a user interface where I can click on certain points in a set of data and save them into a list that can be used later in my code. The problem is it is currently sluggish. Is ...
11
votes
1answer
249 views

Why does Outer sometimes return a packed array, and sometimes not?

For example In[342]:= test3 = Flatten[Outer[Plus, Range[1000], Range[1000]], 1]; // AbsoluteTiming Out[342]= {0.0198735, Null} returns PackedArray (...
11
votes
3answers
274 views

Can I return lists with different dimensions from a compiled function?

Sometimes I'll need several lists with different dimensions to be returned by a compiled function, but compiled function will fail if an irregular list contained: ...
11
votes
1answer
138 views

Bad interaction between Join and ListPlot (or Plot)

When you write this code: tab = {{1, 1}}; tab = Join[tab, {{2,2}}]; you expect to get the following table: {{1, 1}, {2, 2}} ...
11
votes
2answers
1k views

Flatten at a certain level

In an earlier question the brainteasing fourth syntax variant of Flatten was discussed. I see that with ...
11
votes
1answer
154 views

Is there is a design problem or documentation problem with FirstCase?

In version 10 the new function FirstCase can take up to 4 arguments (plus option). The third argument is a default value to be returned if there is no match: ...
11
votes
1answer
445 views

How to merge Datasets representing matrices with named rows/columns?

I am working with square (non-symmetric) matrices with named rows and columns. The data I receive is sometimes messy and rows/columns may come in various permutations. I usually need to semi-...
10
votes
8answers
849 views

Best way to emulate MATLAB's bsxfun using Map and similar functions

Given a matrix, we want to subtract the mean of each column, from all entries in that column. So given this matrix: ...
10
votes
8answers
1k views

Position of First nonzero number

I want to know is there any one line code to find position of first nonzero number without calculating position of all non zero numbers. For example, ...
10
votes
9answers
666 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
6answers
682 views

Flatten nested lists

I have a list as follows lis= {a, {{{b}, {c,d,e}}, {{f}, {g,h,i}}}} which I would like to flatten to ...
10
votes
6answers
511 views

Unique Element in List

I have a list a, and want to find out whether there is a unique element in the list. For instance a={x,y,z,y,x} has an unique ...
10
votes
8answers
632 views

How to use Map inside MapThread?

Given this input lst1 = {{a, b, c}, {d, e, f}}; lst2 = {1, 2}; and the goal is to generate this output ...
10
votes
10answers
685 views

Matrix indexed by one number

I'm trying to find a generic way (for arbitrary dimensions) to create a matrix like so a[1] a[2] a[3] a[4] i.e where it is indexed by one number, instead of say ...
10
votes
4answers
308 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
3answers
645 views

Creating a simple function to compute the average of the difference between pairs of elements in an array

I have a list of the form: {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}} How can I create a function which will take each set at the highest level of ...
10
votes
4answers
2k views

How to sum up subelements of a list of unknown length

I'm simply looking for the {}+{} function for sublist elements of a list: (Parallel sums of elements) ...
10
votes
5answers
668 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
4answers
778 views

How to rebuild original data from a histogram?

I have data in the following form. I have a list of values and a list of frequencies that indicate how often each value has occurred. Example list of values: ...
10
votes
6answers
587 views
10
votes
4answers
552 views

Partitioning a list by recursion (Programming Paradigms via Mathematica (A First Course))

I am working through the course Programming Paradigms via Mathematica. One of the homework exercises for the section Recursion I: Passing the Buck is as follows: Use recursion to partition a list:...
10
votes
5answers
631 views

Span a function across several consecutive elements in a list

Suppose that I have a list called list. I would like to span an arbitrary function f across specified consecutive elements in <...
10
votes
5answers
596 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
7answers
272 views

Extract left-upper triangular data from a square matrix-form list

Hopefully, this question is not too basic or obscure. I'd like an idea on how to extract this {{1}, {5, 2}, {9, 6, 3}, {13, 10, 7, 4}} from ...
10
votes
5answers
1k views

How to subtract one list from another, treating each elements as distinct, assuming the smaller list is a subset of the larger list? [duplicate]

Suppose that I have two lists of different sizes, the smaller of which is a subset of the larger, e.g. a = {"A", "A", "A", "B", "B", "C"} b = {"A", "B"} How can ...
10
votes
3answers
944 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
5answers
439 views

Finding time-series direction reversal of certain magnitude

I'd like to find the points of a time-series that are a certain distance away (in value, not in time) from the previous maximum, which I consider a reversal. For example, for ...
10
votes
4answers
337 views

Efficient way to make subsets of list with placeholders

I have an arbitrary list of unique elements: lst = {a, b, c, d} Documentation allows finding subsets with same number of elements, say ...
10
votes
4answers
202 views

Timing problem with ReplaceRepeated

I have the following data: ...
10
votes
5answers
426 views

Repeating Digits

I have written ...
10
votes
8answers
431 views

Fast way to get positions of “boxed” array elements?

Given a 2D array (arbitrary numeric / evaluates to numeric elements, simplified for examples, and can be arbitrary dimensions so long as the operation makes sense, I'm working with 2K X 2K in my ...
10
votes
3answers
424 views

Modifying strings in a list efficiently

I have a list of strings $\{s_1,s_2,\ldots,s_n\}$. I would like to to produce the following list of strings: $$ \{s_1<>1,\ 1<>s_2<>1,\ldots,\ 1<>s_n\} $$ In other words, I want ...
10
votes
5answers
339 views

Split according to List and apply rule

I have a list which looks some thing like this {a1, a2, a3, a4, a5, a6, a7, a8, a9, a10} Now I want to split it according to lengths ...
10
votes
5answers
444 views

How to randomly select subsets

I have a list of around 300 elements. I want to sample subsets of length 25 such that my samples are all distinct. My first inclination was to use something like ...
10
votes
4answers
390 views

Complement of multisets

Given lists $a$ and $b$, which represent multisets, how can I compute the complement $a\setminus b$? I'd like to construct a function xunion that returns the ...
10
votes
5answers
670 views

Count consecutive occurrences in a list above a certain value

I am trying to tally and sort the number of consecutive occurrences in a list above a certain value. For instance: ...
10
votes
3answers
950 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 ...
10
votes
2answers
346 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
4answers
295 views

Convert elements of list to string

how easily can I convert elements of this list to string. If I have output like this: {{M, 1, 0, 0, 1, 1, 1, 0}, {A, 0, 0, 0}} And I want something like this: <...
10
votes
3answers
769 views

Using Partition to allow sublists of different lengths

As a minimal example, suppose I have a list of the integers from 1 to 25. Suppose I want to use Partition to partition the list into sublists of length 10 but ...
10
votes
4answers
268 views

How to make FlattenAt work with Span?

This works: MapAt[h, {a, {b, c}, {d, e}, {f}}, 2 ;; 3] (* {a, h[{b, c}], h[{d, e}], {f}} *) but this doesn't: ...