New answers tagged

1 vote

Multiple columns from a single column of data file

If I understand what you seek, then with z = Range[32] for clarity, maybe: ...
user avatar
  • 60.1k
1 vote

Multiple columns from a single column of data file

First save a list of random numbers to a file to create a mock experiment: alist = RandomReal[{0, 1}, 100] Export["C:/alist.txt", alist, "List"] ...
user avatar
  • 16.2k
1 vote

How to replace anti diagonal elements of a matrix

Making use of the method posted by ciao (in an answer to How to zero (or replace) the diagonal of a square matrix?) ...
user avatar
  • 10.5k
1 vote

How to select in the following way?

Using MapThread: ...
user avatar
  • 16.2k
2 votes

How to replace anti diagonal elements of a matrix

idm = IdentityMatrix[Length@m] (res = Reverse /@ (m idm) + m (-(Reverse /@ idm ) + 1) ) // MatrixForm $$\left( \begin{array}{ccccc} 100 & 9 & 3 & 8 &...
user avatar
  • 16.2k
2 votes

How to replace anti diagonal elements of a matrix

use element-wise operation. keep the diag-matrix and reverse it. anti = mat // Diagonal // (*take diag*) DiagonalMatrix // (*make mat*) Map[Reverse] (*rev it*) {...
user avatar
  • 6,807
3 votes

How to replace anti diagonal elements of a matrix

We can subtract the anti-diagonal to remove it and then add the Reverse of diagonal. ...
user avatar
  • 32.6k
6 votes

How to replace anti diagonal elements of a matrix

From The Procedural Dodo: If you can do it in place (destroying the orginal m), this will be fast: ...
user avatar
  • 216k
2 votes

How to replace anti diagonal elements of a matrix

With[ {mask = Reverse[ReplaceAll[DiagonalMatrix[Function /@ Diagonal[m]], 0 -> Identity], 2]}, MapThread[Construct, {mask, m}, 2]]
user avatar
  • 5,214
4 votes

How to replace anti diagonal elements of a matrix

Using ReplacePart: ...
user avatar
4 votes
Accepted

How to replace anti diagonal elements of a matrix

This is the kind of question that might trigger the "how-many-ways-can-you-do-it" game. I'll kick it off with this: ...
user avatar
  • 5,214
2 votes

'Mapping' the values of a list to variable

Different way: expr1 = a[n] + b[n] stencil = {-1, 0, 1} f[ex_, sten_] := Sum[Map[# + tp &, ex, {2}] /. tp -> sten[[i]], {i, 1, Length[sten]}] Then, ...
user avatar
  • 890
3 votes
Accepted

Convert List to Matrix

Clear["Global`*"] (mat = RandomInteger[200, {3, 3}]) // MatrixForm Note that the MatrixForm wrapper is isolated ...
user avatar
  • 120k
5 votes
Accepted

How to efficiently rearrange elements in big data file?

The easiest answer is to use Transpose ...
user avatar
  • 29.3k
2 votes

Selecting terms from a list based on certain pattern

n = 3; run = n - (-1) + 1; sel = Flatten@ Normal@SparseArray[{Band[{1, 2}] -> 1, {run, _} -> 1}, {run, run}]; res = Pick[myTab, sel, 1] {{-1, 0}, {0, 1},...
user avatar
  • 16.2k
5 votes
Accepted

Selecting terms from a list based on certain pattern

Using patterns and Cases: ...
user avatar
  • 60.1k
1 vote
Accepted

2D Scatter plot with z-value used for PlotLegends

Using Graphics ...
user avatar
  • 120k
1 vote

Generating Lyndon words modulo mirroring operation and substituion

Here is a bit of code, in case anyone wants to improve on this. Perhaps it also helps clarify what the exact statement of interest is. All code below is completely ad-hoc. (Note that unlike the answer ...
user avatar
1 vote

Generating Lyndon words modulo mirroring operation and substituion

In order to start somewhere, here is my code to generate Lyndon words. First, the following function returns the number of words for a particular n (from oeis.org): ...
user avatar
  • 3,762
1 vote
Accepted

Table mix with If function

I assume you want something like the following: ...
user avatar
1 vote

Write the result of expression to a list

Using Array : Array[2 Cos[# \[Pi]/3] &, 100] also delivers the same output. If you don't want to use pure functions or if a ...
user avatar
  • 16.2k
3 votes

2D Scatter plot with z-value used for PlotLegends

...
user avatar
  • 16.2k
5 votes

Write the result of expression to a list

You can take advantage of the Listable attribute of the trigonometric functions (ie the fact that they automatically thread over lists, to produce a list of results)...
user avatar
6 votes
Accepted

Write the result of expression to a list

As @LouisB comments, using Table: ...
user avatar
4 votes

Unexpected behaviour of Condition with Cases/MatchQ?

To start with, Condition has semantic meaning in Cases/MatchQ: ...
user avatar
  • 5,214
6 votes

Unexpected behaviour of Condition with Cases/MatchQ?

Condition[a, b] is pattern that matches a provided b is ...
user avatar
  • 216k
1 vote
Accepted

Divide string into a sublist of sentence with the maximum number of character

Okay, reading your updated question, maybe this is what you want: Starting with some sample data to test with: ...
user avatar
  • 5,214
2 votes

Divide string into a sublist of sentence with the maximum number of character

Let's get some text from Charles Dicken's Oliver Twist. Define: ...
user avatar
  • 16.2k
4 votes

Divide string into a sublist of sentence with the maximum number of character

Starting with your data: string = "This is the first sentence. This is the second sentence. This is the third sentence." There are two (at least :) ) ...
user avatar
  • 5,214
5 votes

Replacing large numbers

Using Condition: l /. x_Integer /; x > 10^6 -> 0 (*{{1, 2, 3}, {4, 0, 5}}*) Or in the same way: ...
user avatar
15 votes

Replacing large numbers

You could use 3-arg Clip: Clip[l, {-Infinity, 10^10}, {0, 0}] {{1, 2, 3}, {4, 0, 5}} where I used ...
user avatar
  • 124k
2 votes

Replacing large numbers

l = {{1, 2, 3}, {4, 10^(16), 5}} Define a high, low threshold as well as a substitute. thlow = 2; thhi = 3; subst = 11; Find ...
user avatar
  • 16.2k
2 votes

Replacing large numbers

I'm assuming you want something generalizable. Let's say your threshold is 10^10, then something like this: Map[If[# > 10^10, 0, #] &, l, {2}] You could ...
user avatar
  • 5,214
6 votes

How to generate a list of random numbers with conditional statements

SeedRandom[1]; rlist = RandomReal[{1, 20}, 10]; {16.5304, 3.11697, 16.001, 4.56826, 5.58586, 2.24904, 11.3027, 5.39194, 8.52412, 14.309} Using ...
user avatar
  • 16.2k
5 votes

How to generate a list of random numbers with conditional statements

One way "to generate a list of numbers with certain conditions" is to create an ImplicitRegion based on your constraints, and then use ...
user avatar
  • 29.3k
12 votes
Accepted

What is the best method to search pattern for 2D list?

You could use ArrayFilter: ...
user avatar
  • 5,214
7 votes

What is the best method to search pattern for 2D list?

another array-based method: ...
user avatar
  • 6,807
10 votes

What is the best method to search pattern for 2D list?

I am sure there are many ways to do it, but here is my take: ...
user avatar
  • 16.2k
1 vote

How can I multiply only a few columns of a table by some number in a compact way?

Building a MultiplyByPosition function: ...
user avatar
13 votes

Simple, fast re-defining a part of 2D list

Another option is to use ReplacePart ...
user avatar
  • 109k
11 votes

Simple, fast re-defining a part of 2D list

...
user avatar
  • 216k
4 votes

Piecewise Functions from List of Functions

Hooray, I figured it out! Only took 5 hours of struggling for this... ...
user avatar
4 votes

From list shown below - highlight in BOLD the pair with the highest value

You can highlight the largest case with MapAt: MapAt[Style[#, Bold] &, list, OrderingBy[list, Last, -1]] In the last ...
user avatar
  • 5,444
1 vote

How can I multiply only a few columns of a table by some number in a compact way?

MapAt[a*#&,tab,{{All,2},{All,5}}]
user avatar
  • 5,214
1 vote
Accepted

Why does my simple string function fail to grab the only element in a List?

I recommend handling the String vs. List arguments with alternative definitions of your function. That would simplify your work, ...
user avatar
  • 60.1k
9 votes

How can I multiply only a few columns of a table by some number in a compact way?

Using the Dot product: tab.DiagonalMatrix[{1,2,1,1,2,1}] Or, if there are a lot of columns: Edit Lucas Lang suggested a neat ...
user avatar
  • 10.5k
5 votes

How can I multiply only a few columns of a table by some number in a compact way?

a = 2; k = 2; m = 5; Using direct multiplication: ...
user avatar
  • 16.2k
18 votes

How can I multiply only a few columns of a table by some number in a compact way?

Another option could be a*tab[[All, {2, 5}]] Which will multiply columns 2 and 5 by a. To replace the original matrix just do <...
user avatar
  • 109k
16 votes
Accepted

How can I multiply only a few columns of a table by some number in a compact way?

The simplest method I can think off, is with Query: Query[All, Thread[{2, 5} -> Function[a * #]]] @ tab
user avatar
  • 18.2k
4 votes

Partitioning a list based on a criterion for sublists

This should be the fastest so far ...
user avatar

Top 50 recent answers are included