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

learn more… | top users | synonyms (4)

10
votes
3answers
488 views

Zero-dimensional matrices

In a recent Mathematica project, $(n \times 0)$- and $(0 \times n)$-dimensional matrices have suddenly become a frustratingly common edge case for me. For instance, consider the following two ...
5
votes
4answers
269 views

How to split a list with respect to its nested sublists?

I have a problem with the following task: having some nested list list = {{k6},{k1,k3,k5},{k7}} I would like to replace all sublists by their permutations and ...
15
votes
4answers
927 views

List comprehension in Mathematica

Python supports a concept called "list comprehensions". Following is sample example ...
10
votes
3answers
255 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: ...
5
votes
1answer
187 views

Can the elements of a list always be sorted into a canonical order?

As the title states - does a unique order exist for any Mathematica list to be sorted by the Sort[] function (or as returned by a function that treats lists as ...
5
votes
1answer
189 views

MapThread over large graph

I am trying to build a very large graph with approximately $16\,000\,000$ edges, each being a two-dimensional integer vector. I have a list of edges in the form $\{\{e_{11}, e_{12}\}, \{e_{21}, ...
0
votes
2answers
311 views

Replace all elements of a list

I have a set that looks like: s = {{1, 2, 3} -> 9, {4, 1, 9} -> 9, {3, 7, 3} -> 1}; Now, I want to replace each element of the set by the lhs of "...
13
votes
6answers
683 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 ...
4
votes
2answers
635 views

Removing not-a-number entries from an array of numbers

I have a list of {x, y, z} coordinates from a wavefront sensor I'm working with. However, not all of the imported points have actual values, but they still get ...
1
vote
2answers
286 views

Deleting parts from a nested list

I have a very long nested list. Here is a short example: list = {{1.2, {1, 1, 1, 1}, {2, 2, 2}}, {0.9, {3, 3, 3, 3}, {4, 4, 4}}, {1.4, {4, 4, 4, 4}, {5, 5, 5}}} ...
8
votes
4answers
342 views

Generate PrimePower counting function

Is there a way to generate a counting function for prime powers - i.e. to create a similar function to PrimePi, but including prime powers. The following will, of ...
2
votes
3answers
160 views

How to do subtraction with sets in which the elements have multiplicity indicators? [duplicate]

I want to do subtraction with sets l1 and l2, which contain multiplicity indicators. For example, ...
2
votes
1answer
872 views
6
votes
3answers
260 views

Loop function over list [duplicate]

I would like to divide element 1 of a list by element 2; element 2 by element 3, and so on. What is the best way of doing this? I have tried f = #1/#2 & ...
6
votes
3answers
414 views

Mapping a function that assigns values to a list over a different list

I'm trying to get used to Mathematica's approach to programming and list manipulation, and am struggling with one thing. Say that I have a function f which takes a ...
1
vote
1answer
179 views

How should I go about writing more functional and list based code? [duplicate]

I've heard that Mathematica is usually faster when the code is list-based and/or uses a functional paradigm. I'm still working on getting the hang of this; there are a lot of ...
2
votes
2answers
131 views

Deleting elements on a list by the first element of another list

I was trying to make this L1 = {{2, 1}, {6, 4}, {8, 7}, {9, 5}}; L2 = {{2, 9}, {9, 6}}; L = Union[L2 , L1] And I used this: ...
19
votes
5answers
583 views

How to split list into disjoint lists?

I wonder if there is any simple way to split a list into its disjoint sublists, e.g. from list={{a,b},{e,f},{b,c},{c,d},{f,g,h}} get a result ...
0
votes
2answers
119 views

Code for sum of exponential divisors function

Consider $n = p_{1}^{a_1}\cdots p_{r}^{a_{r}}$. An integer $d = p_{i}^{b_{i}} \cdots p_{r}^{b^{r}}$ is called an exponential divisor of $n$ if $b_{i}$ divides $a_{i}$ for every $1\leq i \leq r.$ I am ...
0
votes
3answers
1k views

Find the number of nonzero elements

How do you find the number of elements in a matrix that are non-zero. For instance, the following matrix has 5 nonzero elements. ...
1
vote
1answer
127 views

How to apply compiled function on list

Compile function output error: How to apply compiled function on list? ...
2
votes
1answer
91 views

Convert Integer to Numeric with Replacement rules

I have a long list of triples, each looking something like {AGO, 1988, 2345.23}. Some of these, however, have an integer in the third spot, like this: ...
1
vote
0answers
289 views

Using Part within a Compiled Command

I'm looking to define a multi-line function that makes use of the "Part" command. A toy example: ...
2
votes
0answers
71 views

Produce {f[x],g[y],h[z]} in as many ways as possible [duplicate]

I have two lists of equal lengths but for my task I will use 3-element lists , namely {x,y,z} and {f,g,h}. The first contains ...
0
votes
1answer
251 views

Performing matrix multiplications with a list

I have a question in regards to PseudoInverse. I have $A$, an $n\times 2$ matrix, and when I want to compute $(A^T A)^{-1}A$, by using ...
4
votes
3answers
503 views

efficient way to give nearest neighbour and next nearest neighbour of every point in a point set

Suppose we have a point set, the points are labeled 1,2,3,....n. And p[i] is the coordinate of the point. Now I want the label of nearest and next nearest point ...
4
votes
2answers
5k views

How to combine two vectors into a matrix by columns?

I know it may sounds idiot, but I've been looking through the document, and couldn't find a way to combine two vectors say {a, b} and ...
8
votes
7answers
854 views

How to partition a list in a specific way

I have a list: a = {{1, 2, 3, 4, 1, 2, 3, 4}, {5, 6, 7, 8, 5, 6, 7, 8}}; Using Partition and ...
0
votes
0answers
43 views

Store results of inequality [duplicate]

I eveluated some values using Reduce commend and I store them in a Table ...
18
votes
9answers
1k views

How to replace an element in a list based on the value of the next element?

I use Mathematica a little bit. I try to understand it, but it is hard I have a list of 5 numbers selected randomly from 0 and -1. I have to replace a 0 with a 1 when the 0 is followed by -1. My ...
11
votes
10answers
554 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 ...
4
votes
1answer
216 views

Efficient ways to rotate a ListPlot?

I have a numerical solution from a differential equation that is in the form of a list, in $(x,z)$-plane. I would like to rotate the 2D ListPlot around the $z$-axis ...
9
votes
5answers
3k 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 ...
6
votes
2answers
217 views
11
votes
1answer
261 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 ...
0
votes
1answer
114 views

How to create 2k sized vector in k sized loop

I want to solve a system of simple differential equations. At some point I need to solve non-linear system of algebraic equations with Newton method. I would like to efficiently create a right hand ...
20
votes
15answers
1k views

How do I extract the middle element(s) of a given list?

How do I extract the middle element(s) of a given list? Here is some code that works, but seems a little too long: ...
1
vote
2answers
119 views

How to return matching sublists or empty list

How to return matching sublists or empty list m1 = {{1, 0.5}, {2, 0.4}, {3, 0.1}}; m2 = {{1, 0.5}, {0, 0.4}, {0, 0.1}}; Commonest[Join[m1, m2]] out: ...
2
votes
2answers
177 views

Organise data in table

I have a large amount of data available (in excel), which is not very organised. I can do this by copy paste, but I am trying to do this using mathematica. Hopefully someone can help me. I imported ...
3
votes
2answers
812 views

How can I scale the y axis?

I have a file I import and read into a table of $2 \times n$. I can plot this data using the following code: ...
-2
votes
1answer
86 views

Using Product for different variables

when using product in mathematica the initial and maximum value of the product will be the variable itself that will be maupulated. what I need is to make these initial and maximum of the product as ...
1
vote
3answers
222 views

Code hanging inside a For loop

I'm pretty new to Mathematica (and functional programming in general). I'm working on some code to find winners (maximal values) in a list. The code I have that is hanging is this line. ...
2
votes
1answer
494 views

RowReduce: Solving for the resource vector (a, b, c) in Augmented Matrix

Here are two examples: RowReduce[{{3, 1, a}, {2, 1, b}}] evaluates to {{1, 0, a - b}, {0, 1, -2 a + 3 b}} but ...
8
votes
8answers
503 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 ...
3
votes
6answers
363 views

Compute ratios of elements in two lists

I have two lists, say a and b, both of length n. I'd like to compute the following: ...
-1
votes
2answers
241 views

How can I extract when a select condition is met in a do loop the value of the iterator?

I use the following code to find out the number of consecutive prime numbers using a formula $n^2+n+i$ found out by Euler (starting from n=0): ...
18
votes
2answers
4k views

Is there an equivalent to MATLAB's linspace? [duplicate]

Does Mathematica have an equivalent to Matlab's linspace? I want to make a list with "start", "stop" and "number of points". What is the neatest way of doing this?
5
votes
5answers
220 views

Reversing every second “row” in a list

I have a list that is obtained by flattening a matrix list=Flatten[Table[{i, j}, {i, 1, 4}, {j, 2, 6, 2}], 1] {{1, 2}, {1, 4}, {1, 6}, {2, 2}, {2, 4}, {2, 6}, ...
2
votes
2answers
90 views

How can I generate a variable-length list of options in PieChart?

I would like to allow a user to manipulate the segments of a pie-chart, which is to be generated from a list of a specific format. The code looks something like the following: ...
0
votes
0answers
66 views