Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 45431

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

4 votes

Finding the maximum from a two dimensional list

Here's a variation of @sjoerd's answer: data={{1,2},{1,3},{1,4},{2,2},{2,3},{2,4},{2,5}}; ResourceFunction["GroupByList"][data[[All,2]],data[[All,1]],Max] <|1 -> 4, 2 -> 5|> For large datasets, th …
Carl Woll's user avatar
  • 132k
5 votes
Accepted

Overbar as counter in a loop

You could give OverBar the HoldFirst attribute so that it doesn't depend on the value of a: SetAttributes[OverBar,HoldFirst] Then: t1 = Table[ p[a, OverBar[a], b, OverBar[b]], {a, 0, 1}, {Ove …
Carl Woll's user avatar
  • 132k
7 votes

Count the times a digit has appeared in a list as I scan the list

Compiling is usually the best approach for problems like this where an iteration depends on previous state. Here is a compiled function to get the counts (basically a compiled version of the approache …
Carl Woll's user avatar
  • 132k
1 vote
Accepted

Delete terms from set that met a stated condition

It's unclear what the Mathematica form of the set is. I will assume that the set elements are just symbols: DeleteDuplicates[ {ab,cd,cea,abc,bcd}, StringContainsQ[SymbolName[#2],SymbolName[#1 …
Carl Woll's user avatar
  • 132k
3 votes
Accepted

Listing what has been deleted by DeleteDuplicatesBy

Based on @Coolwater's fine answer to Replace element in array by checking condition in another list you can use GatherBy: Catenate @ GatherBy[ {{a,b},{f,c},{d,b},{e,c},{a,c}}, Last ][[All, 2; …
Carl Woll's user avatar
  • 132k
1 vote

Truncating a list once all members of a set have appeared?

If the list consists of positive integers smaller than some reasonable maximum, then the following function should be pretty fast: trunc[a_, b_] := Module[{c = ConstantArray[0, Max[a]]}, c[[Rever …
Carl Woll's user avatar
  • 132k
7 votes

Cumulative sum of consecutive positive or negative numbers

Since you mentioned you are working with huge arrays, you might consider using Compile. For instance, here are two possible Compile approaches: fc = Compile[{{a, _Integer}, {b, _Integer}}, If[a b …
Carl Woll's user avatar
  • 132k
2 votes

Unsorted custom intersection

You could use Nearest for determining intersection1. Here is some sample data: SeedRandom[3]; list1=RandomComplex[1+I, 10^3]; list2=RandomComplex[1+I, 10^4]; Create the Nearest function: nf = Near …
Carl Woll's user avatar
  • 132k
2 votes

Sum of two columns of 2 tables

Here is an example of your tables, since you didn't provide one: tb1 = Table[{r,f1[r]}, {r, {r1,r2,r3}}] tb2 = Table[{r,f2[r]}, {r, {r1,r2,r3}}] tb3 = Table[{r,f3[r]}, {r, {r1,r2,r3}}] {{r1, f1[ …
Carl Woll's user avatar
  • 132k
8 votes

Part of the nested lists

You could "transpose" your lst using Flatten (1), and then take the 4 element: Flatten[lst, {{2}, {1}}][[4]] {d, h} Flatten command: matrix as second argument
Carl Woll's user avatar
  • 132k
3 votes

Is there a function which instantly tells you whether an element is part of a list?

In M11.1+ (perhaps earlier as well) you can use Nearest to test multiple elements at once. Here is a comparison with @LeonidShifrin's answer: lrgTest = RandomInteger[100000,{100000}]; r1 = Nearest[l …
Carl Woll's user avatar
  • 132k
3 votes

How to get this list with a terse method

Not as fast as some of the other methods, but a TranslationTransform approach is also possible: TranslationTransform[{a,b}][{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}] {{-1 + a, b}, {1 + a, b}, {a, -1 + …
Carl Woll's user avatar
  • 132k
7 votes
Accepted

List multiplication

Convert your vector to a diagonal matrix, and then use Dot: data = RandomReal[1,{13000,3}]; data1 = data . DiagonalMatrix[{x, y, z}]; //RepeatedTiming {0.000051, Null} Another very fast approa …
Carl Woll's user avatar
  • 132k
17 votes
Accepted

Subtract value from list

I would use Clip: Clip[{2,3,5,4,6,7,3,7,5,4}, {5, Infinity}, {0, 0}] {0, 0, 5, 0, 6, 7, 0, 7, 5, 0}
Carl Woll's user avatar
  • 132k
3 votes
Accepted

Arguments of Array function in prefix form

This is basically a duplicate of pure functions in nested select. Applying the accepted answer to your question: list = {100, 200, 300, 400, 500}; Array[Function[x, #[[x]]], Length[#]]& @ list { …
Carl Woll's user avatar
  • 132k

1
2 3 4 5
25
15 30 50 per page