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

learn more… | top users | synonyms (3)

14
votes
4answers
2k views

Semi prime numbers

The high school textbook I am using has the example of semi-prime numbers. They wanted students to find (by "perspiration") all the semi-prime numbers less than $50$ (for a question on set theory). ...
14
votes
6answers
464 views

Splitting a list by specifying section headers

I have a list of strings called mylist: mylist = {"[a]", "a", "a", "[b]", "b", "b", "[ c ]", "c", "c"}; I would like to split ...
14
votes
6answers
635 views

Matrix Rotation

If I have a matrix of any size, say $\begin{pmatrix} 72 & 32 & 64 \\ 18 & 8 & 16 \\ 63 & 28 & 56 \\ \end{pmatrix}$ $\begin{pmatrix} 72 & 32 \\ 18 & 8 \\ 63 ...
14
votes
7answers
646 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 ...
14
votes
4answers
633 views

Implementing a function which generalizes the merging step in merge sort

One of the key steps in merge sort is the merging step. Given two sorted lists sorted1={2,6,10,13,16,17,19}; sorted2={1,3,4,5,7,8,9,11,12,14,15,18,20}; of ...
14
votes
4answers
557 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 ...
14
votes
6answers
481 views

Elegantly pairing up mismatched lists

HistogramList returns a list of bin boundaries and a list of counts. There is one more boundary than counts, and I'd like to pair them up so I can feed it into ...
14
votes
3answers
523 views

Determining all possible traversals of a tree

I have a list: B={423, {{53, {39, 65, 423}}, {66, {67, 81, 423}}, {424, {25, 40, 423}}}}; This list can be visualized as a tree using ...
14
votes
2answers
519 views

Reproducing nested loops using Map?

I keep producing bits of code like the following: ...
14
votes
3answers
404 views

Quick multiple selections from a list

What is the fastest way to make multiple selections from a list? Compiled methods included. For example, here are two methods for selecting a subset, compared:- ...
14
votes
1answer
213 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: ...
14
votes
1answer
169 views

Why changing list's head takes time?

Consider AbsoluteTiming[Range[10^7];][[1]] 0.035000 and AbsoluteTiming[HoldComplete @@ Range[10^7];][[1]] ...
13
votes
5answers
544 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: ...
13
votes
11answers
885 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: ...
13
votes
9answers
581 views

Thread over list in different levels

I have these two lists, both with the same length in the first dimension: ...
13
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 ...
13
votes
5answers
750 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 : ...
13
votes
7answers
740 views

DeleteCases on list of Strings

Consider the following: data={"AB","CD","AF"}; Now I would like to delete all String from data which starts with "A". ...
13
votes
7answers
753 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 ...
13
votes
6answers
972 views

Splitting up delimited data in lists

One task that I frequently find myself doing in Mathematica is splitting lists into lists of sublists, using specific elements to define the break-points. This is particularly useful with imported ...
13
votes
4answers
812 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 ...
13
votes
7answers
375 views

Get the first element after the first sequence of length N of consecutively increasing values

I'm looking for an efficient way of extracting the first element after the first sequence of N consecutive elements in which the values are increasing. If these are the data of a toy example: ...
13
votes
2answers
335 views

Bad performance of LengthWhile?

A friend of mine showed me this example, it's a test comparing LengthWhile to a self-made lengthwhile written in a direct and ...
12
votes
9answers
447 views

Generating a matrix using sublists A and B n times

I want to write a function that generates a square matrix from sublists. My sublists are a = Range[0, x, 0.5]; b = Range[0.25, x + 0.25, 0.5]; Suppose x=2, then I ...
12
votes
6answers
634 views

Elegantly split a matrix into positive and negative parts?

I have a matrix $M$ of real components, and I want to split it into two matrices $M^+$ and $M^-$ of the same dimensions as $M$, where $M^+$ contains the positive components of $M$ (the remaining ...
12
votes
10answers
618 views

How can I generate a nested array like this one?

Well, this is how the array looks like: I came up with this code: n = 10; Nest[ArrayPad[#, 1, 1 - #[[1, 1]]] &, {{1}}, n] /. {1 -> "*"} Any better ...
12
votes
10answers
675 views

Counting sublists containing 0

Given a list: data = {{0, 2}, {0, 0}, {1, 0}, {1, 2}, {2, 3}}. I would like to count the number of sublists which contain zero. ...
12
votes
9answers
464 views

Dropping n consecutive terms from a list periodically

Suppose I have the following list lis = Range[100]; and I want to remove n consecutive terms periodically from the list. For example suppose I want to drop terms ...
12
votes
5answers
509 views

Filtering elements from a list that appear only once

I want to be able to select elements from a list that only appear once. I originally had this: Select[Table[Count[list, i], {i, list}], # == 1] But the issue is ...
12
votes
7answers
558 views
12
votes
5answers
678 views

What's the cleanest functional-programming style way to perform this list operation?

I am looking for the correct method for accomplishing the unknown piece in the following code snippet: rolls = Tuples[Range[1, 4], {2}] ...
12
votes
9answers
726 views

How to partition a list of numbers in two lists of numbers such that each lists elements sums up to the same number?

I have the following list of squares. {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256} Their sum is $1496$, I want to make two partitions ...
12
votes
6answers
549 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 ...
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 ...
12
votes
5answers
288 views

Can ReplacePart be sped up?

Test case: ...
12
votes
4answers
354 views

How to enumerate all possible binary associations?

Suppose I have a list of symbols like: {a,b,c,d} I would like to enumerate all possible binary associations (combining symbols and/or sublists pairwise): ...
12
votes
7answers
968 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: ...
12
votes
1answer
363 views

How can I collect data for visualization of quick sort?

I know how to implement quick sort in Mathematica. But I don't know how to get the temp data for visualization, because I used recursion. I want the effect similar to this: ...
12
votes
4answers
2k views

Reshape vectors exactly like in MATLAB

This gives a 64×1 column vector in Mathematica: ...
12
votes
5answers
589 views

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

This input: Permutations[Range[12]] Results in this (error) output: ...
12
votes
3answers
349 views

Taking Top K Occurrences

Having millions of numbers in a list, I want to take top K occurrences. MMA Commonest is very slow so I write my own versions: ...
12
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 ...
12
votes
2answers
1k views

How do I find the elements in a list that return the highest value for a function?

Suppose we have a list of values and a function f. I want to find which of the elements maximizes the return value of the function in Mathematica. Let call the ...
12
votes
4answers
336 views

The gap between MapAt and ReplacePart for 2D data tables with headers

Consider a relational table derived from survey data, where each column ("001-01" ...) represents a responder and each row (...
12
votes
5answers
354 views

Faster way to convert triangle list to edge list with no duplicates

I have a list of triangles returned by a Delaunay triangulator, in the following format: ...
12
votes
4answers
506 views

Calculate proportion of values for dates

At work, we were discussing when is it the best time to change to winter tires for bikes and/or cars. Using WeatherData[] and ...
12
votes
2answers
495 views

Are there advantages to using generalized Part extraction instead of specialized functions like First, Last?

There are many ways to extract different parts of lists in Mathematica. For example, the first part of a list v can be accessed either as ...
12
votes
1answer
613 views

How to monitor the progress of Map?

I have a function doSomethingComplicated[...] that takes about 10s on average to evaluate. My list, listOfArgs has about 10000 ...
12
votes
2answers
1k views

How do I split up a curve into segments of equal length?

I have a curve that is defined as f[x] and what I'm attempting to do is to divide the curve into equal straight lengths for a number of segments of my choosing that I've defined as nSeg. I've created ...
12
votes
4answers
376 views

Equivalent Nested Loop Structure

Consider the following examples: ...