Map list to bin numbers Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?quantilization (if that is a word)Map efficiently over duplicates in listGenerating a list of cubefree numbersIs there an equivalent to MATLAB's linspace?Convert a list of hexadecimal numbers to decimalTaking one list Mod a second listlist of items and group of alternative itemsHow find numbers in this list of inequalities?Selecting list entries with a True False index list of similar lengthReplace element in array by checking condition in another listAttempting to fill a table with the number of elements in each bin and make a table with the elements in the bins?
List *all* the tuples!
Why one of virtual NICs called bond0?
How do I keep my slimes from escaping their pens?
Disable hyphenation for an entire paragraph
Do I really need recursive chmod to restrict access to a folder?
Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?
Java 8 stream max() function argument type Comparator vs Comparable
What's the purpose of writing one's academic bio in 3rd person?
Can Pao de Queijo, and similar foods, be kosher for Passover?
If 'B is more likely given A', then 'A is more likely given B'
What is the correct way to use the pinch test for dehydration?
Is 1 ppb equal to 1 μg/kg?
What is the musical term for a note that continously plays through a melody?
What do you call a plan that's an alternative plan in case your initial plan fails?
Is there a service that would inform me whenever a new direct route is scheduled from a given airport?
Single word antonym of "flightless"
Why is black pepper both grey and black?
What are 'alternative tunings' of a guitar and why would you use them? Doesn't it make it more difficult to play?
Antler Helmet: Can it work?
"Seemed to had" is it correct?
Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?
Check which numbers satisfy the condition [A*B*C = A! + B! + C!]
How can I make names more distinctive without making them longer?
Is there a concise way to say "all of the X, one of each"?
Map list to bin numbers
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?quantilization (if that is a word)Map efficiently over duplicates in listGenerating a list of cubefree numbersIs there an equivalent to MATLAB's linspace?Convert a list of hexadecimal numbers to decimalTaking one list Mod a second listlist of items and group of alternative itemsHow find numbers in this list of inequalities?Selecting list entries with a True False index list of similar lengthReplace element in array by checking condition in another listAttempting to fill a table with the number of elements in each bin and make a table with the elements in the bins?
$begingroup$
Does WL have the equivalent of Matlab's discretize or NumPy's digitize? I.e., a function that takes a length-N list and a list of bin edges and returns a length-N list of bin numbers, mapping each list item to its bin number?
list-manipulation data
$endgroup$
|
show 1 more comment
$begingroup$
Does WL have the equivalent of Matlab's discretize or NumPy's digitize? I.e., a function that takes a length-N list and a list of bin edges and returns a length-N list of bin numbers, mapping each list item to its bin number?
list-manipulation data
$endgroup$
$begingroup$
HistogramList
seems similar. This could also be done efficiently withGroupBy
and some easy littleCompile
-d selection determiner. Or maybe hit it first withSort
then write something that only checks the next bin up. Again, can be easilyCompile
-d.
$endgroup$
– b3m2a1
Apr 8 at 23:07
$begingroup$
I need it to work like a map (in terms of the order of the items in the resulting list). Of course it is possible to write something ...
$endgroup$
– Alan
Apr 9 at 0:13
$begingroup$
Related: 140577
$endgroup$
– Carl Woll
Apr 9 at 3:50
1
$begingroup$
Did you tryBinCounts
? I guess it is what you need.
$endgroup$
– Rom38
Apr 9 at 4:52
$begingroup$
@Rom38 You probably meantBinLists
, right?
$endgroup$
– Henrik Schumacher
Apr 9 at 5:55
|
show 1 more comment
$begingroup$
Does WL have the equivalent of Matlab's discretize or NumPy's digitize? I.e., a function that takes a length-N list and a list of bin edges and returns a length-N list of bin numbers, mapping each list item to its bin number?
list-manipulation data
$endgroup$
Does WL have the equivalent of Matlab's discretize or NumPy's digitize? I.e., a function that takes a length-N list and a list of bin edges and returns a length-N list of bin numbers, mapping each list item to its bin number?
list-manipulation data
list-manipulation data
edited Apr 9 at 5:27
user64494
3,60711122
3,60711122
asked Apr 8 at 22:52
AlanAlan
6,6501125
6,6501125
$begingroup$
HistogramList
seems similar. This could also be done efficiently withGroupBy
and some easy littleCompile
-d selection determiner. Or maybe hit it first withSort
then write something that only checks the next bin up. Again, can be easilyCompile
-d.
$endgroup$
– b3m2a1
Apr 8 at 23:07
$begingroup$
I need it to work like a map (in terms of the order of the items in the resulting list). Of course it is possible to write something ...
$endgroup$
– Alan
Apr 9 at 0:13
$begingroup$
Related: 140577
$endgroup$
– Carl Woll
Apr 9 at 3:50
1
$begingroup$
Did you tryBinCounts
? I guess it is what you need.
$endgroup$
– Rom38
Apr 9 at 4:52
$begingroup$
@Rom38 You probably meantBinLists
, right?
$endgroup$
– Henrik Schumacher
Apr 9 at 5:55
|
show 1 more comment
$begingroup$
HistogramList
seems similar. This could also be done efficiently withGroupBy
and some easy littleCompile
-d selection determiner. Or maybe hit it first withSort
then write something that only checks the next bin up. Again, can be easilyCompile
-d.
$endgroup$
– b3m2a1
Apr 8 at 23:07
$begingroup$
I need it to work like a map (in terms of the order of the items in the resulting list). Of course it is possible to write something ...
$endgroup$
– Alan
Apr 9 at 0:13
$begingroup$
Related: 140577
$endgroup$
– Carl Woll
Apr 9 at 3:50
1
$begingroup$
Did you tryBinCounts
? I guess it is what you need.
$endgroup$
– Rom38
Apr 9 at 4:52
$begingroup$
@Rom38 You probably meantBinLists
, right?
$endgroup$
– Henrik Schumacher
Apr 9 at 5:55
$begingroup$
HistogramList
seems similar. This could also be done efficiently with GroupBy
and some easy little Compile
-d selection determiner. Or maybe hit it first with Sort
then write something that only checks the next bin up. Again, can be easily Compile
-d.$endgroup$
– b3m2a1
Apr 8 at 23:07
$begingroup$
HistogramList
seems similar. This could also be done efficiently with GroupBy
and some easy little Compile
-d selection determiner. Or maybe hit it first with Sort
then write something that only checks the next bin up. Again, can be easily Compile
-d.$endgroup$
– b3m2a1
Apr 8 at 23:07
$begingroup$
I need it to work like a map (in terms of the order of the items in the resulting list). Of course it is possible to write something ...
$endgroup$
– Alan
Apr 9 at 0:13
$begingroup$
I need it to work like a map (in terms of the order of the items in the resulting list). Of course it is possible to write something ...
$endgroup$
– Alan
Apr 9 at 0:13
$begingroup$
Related: 140577
$endgroup$
– Carl Woll
Apr 9 at 3:50
$begingroup$
Related: 140577
$endgroup$
– Carl Woll
Apr 9 at 3:50
1
1
$begingroup$
Did you try
BinCounts
? I guess it is what you need.$endgroup$
– Rom38
Apr 9 at 4:52
$begingroup$
Did you try
BinCounts
? I guess it is what you need.$endgroup$
– Rom38
Apr 9 at 4:52
$begingroup$
@Rom38 You probably meant
BinLists
, right?$endgroup$
– Henrik Schumacher
Apr 9 at 5:55
$begingroup$
@Rom38 You probably meant
BinLists
, right?$endgroup$
– Henrik Schumacher
Apr 9 at 5:55
|
show 1 more comment
3 Answers
3
active
oldest
votes
$begingroup$
Here's a version based on Nearest
:
digitize[edges_] := DigitizeFunction[edges, Nearest[edges -> "Index"]]
digitize[data_, edges_] := digitize[edges][data]
DigitizeFunction[edges_, nf_NearestFunction][data_] := With[init = nf[data][[All, 1]],
init + UnitStep[data - edges[[init]]] - 1
]
For example:
SeedRandom[1]
data = RandomReal[10, 10]
digitize[data, 2, 4, 5, 7, 8]
8.17389, 1.1142, 7.89526, 1.87803, 2.41361, 0.657388, 5.42247, 2.31155, 3.96006, 7.00474
5, 0, 4, 0, 1, 0, 3, 1, 1, 4
Note that I broke up the definition of digitize
into two pieces, so that if you do this for multiple data sets with the same edges
list, you only need to compute the nearest function once.
$endgroup$
add a comment |
$begingroup$
This is a very quick-n-dirty, but may serve as a simple example.
This creates a piecewise function following the first definition in Matlab's discretize documentation, then applies that to the data.
disc[data_, edges_] := Module[e = Partition[edges, 2, 1], p, l,
l = Length@e;
p=Piecewise[Append[Table[i, e[[i, 1]] <= x < e[[i, 2]], i, l - 1]
, l,e[[l, 1]] <= x <= e[[l, 2]]]
, "NaN"];
Table[p, x, data]];
From the first example in the above referenced documentation:
data=1, 1, 2, 3, 6, 5, 8, 10, 4, 4;
edges=2, 4, 6, 8, 10;
disc[data,edges]
NaN,NaN,1,1,3,2,4,4,2,2
I'm sure there are more efficient/elegant solutions, and will revisit as time permits.
$endgroup$
add a comment |
$begingroup$
You may also use Interpolation
with InterpolationOrder -> 0
. However, employing Nearest
as Carl Woll did will usually be much faster.
First, we prepare the interplating function.
m = 20;
binboundaries = Join[-1., Sort[RandomReal[-1, 1, m - 1]], 1.];
f = Interpolation[Transpose[binboundaries, Range[0, m]], InterpolationOrder -> 0];
Now you can apply it to lists of values as follows:
vals = RandomReal[-1, 1, 1000];
Round[f[vals]]
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194844%2fmap-list-to-bin-numbers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Here's a version based on Nearest
:
digitize[edges_] := DigitizeFunction[edges, Nearest[edges -> "Index"]]
digitize[data_, edges_] := digitize[edges][data]
DigitizeFunction[edges_, nf_NearestFunction][data_] := With[init = nf[data][[All, 1]],
init + UnitStep[data - edges[[init]]] - 1
]
For example:
SeedRandom[1]
data = RandomReal[10, 10]
digitize[data, 2, 4, 5, 7, 8]
8.17389, 1.1142, 7.89526, 1.87803, 2.41361, 0.657388, 5.42247, 2.31155, 3.96006, 7.00474
5, 0, 4, 0, 1, 0, 3, 1, 1, 4
Note that I broke up the definition of digitize
into two pieces, so that if you do this for multiple data sets with the same edges
list, you only need to compute the nearest function once.
$endgroup$
add a comment |
$begingroup$
Here's a version based on Nearest
:
digitize[edges_] := DigitizeFunction[edges, Nearest[edges -> "Index"]]
digitize[data_, edges_] := digitize[edges][data]
DigitizeFunction[edges_, nf_NearestFunction][data_] := With[init = nf[data][[All, 1]],
init + UnitStep[data - edges[[init]]] - 1
]
For example:
SeedRandom[1]
data = RandomReal[10, 10]
digitize[data, 2, 4, 5, 7, 8]
8.17389, 1.1142, 7.89526, 1.87803, 2.41361, 0.657388, 5.42247, 2.31155, 3.96006, 7.00474
5, 0, 4, 0, 1, 0, 3, 1, 1, 4
Note that I broke up the definition of digitize
into two pieces, so that if you do this for multiple data sets with the same edges
list, you only need to compute the nearest function once.
$endgroup$
add a comment |
$begingroup$
Here's a version based on Nearest
:
digitize[edges_] := DigitizeFunction[edges, Nearest[edges -> "Index"]]
digitize[data_, edges_] := digitize[edges][data]
DigitizeFunction[edges_, nf_NearestFunction][data_] := With[init = nf[data][[All, 1]],
init + UnitStep[data - edges[[init]]] - 1
]
For example:
SeedRandom[1]
data = RandomReal[10, 10]
digitize[data, 2, 4, 5, 7, 8]
8.17389, 1.1142, 7.89526, 1.87803, 2.41361, 0.657388, 5.42247, 2.31155, 3.96006, 7.00474
5, 0, 4, 0, 1, 0, 3, 1, 1, 4
Note that I broke up the definition of digitize
into two pieces, so that if you do this for multiple data sets with the same edges
list, you only need to compute the nearest function once.
$endgroup$
Here's a version based on Nearest
:
digitize[edges_] := DigitizeFunction[edges, Nearest[edges -> "Index"]]
digitize[data_, edges_] := digitize[edges][data]
DigitizeFunction[edges_, nf_NearestFunction][data_] := With[init = nf[data][[All, 1]],
init + UnitStep[data - edges[[init]]] - 1
]
For example:
SeedRandom[1]
data = RandomReal[10, 10]
digitize[data, 2, 4, 5, 7, 8]
8.17389, 1.1142, 7.89526, 1.87803, 2.41361, 0.657388, 5.42247, 2.31155, 3.96006, 7.00474
5, 0, 4, 0, 1, 0, 3, 1, 1, 4
Note that I broke up the definition of digitize
into two pieces, so that if you do this for multiple data sets with the same edges
list, you only need to compute the nearest function once.
edited Apr 9 at 3:52
answered Apr 9 at 3:33
Carl WollCarl Woll
74.2k398193
74.2k398193
add a comment |
add a comment |
$begingroup$
This is a very quick-n-dirty, but may serve as a simple example.
This creates a piecewise function following the first definition in Matlab's discretize documentation, then applies that to the data.
disc[data_, edges_] := Module[e = Partition[edges, 2, 1], p, l,
l = Length@e;
p=Piecewise[Append[Table[i, e[[i, 1]] <= x < e[[i, 2]], i, l - 1]
, l,e[[l, 1]] <= x <= e[[l, 2]]]
, "NaN"];
Table[p, x, data]];
From the first example in the above referenced documentation:
data=1, 1, 2, 3, 6, 5, 8, 10, 4, 4;
edges=2, 4, 6, 8, 10;
disc[data,edges]
NaN,NaN,1,1,3,2,4,4,2,2
I'm sure there are more efficient/elegant solutions, and will revisit as time permits.
$endgroup$
add a comment |
$begingroup$
This is a very quick-n-dirty, but may serve as a simple example.
This creates a piecewise function following the first definition in Matlab's discretize documentation, then applies that to the data.
disc[data_, edges_] := Module[e = Partition[edges, 2, 1], p, l,
l = Length@e;
p=Piecewise[Append[Table[i, e[[i, 1]] <= x < e[[i, 2]], i, l - 1]
, l,e[[l, 1]] <= x <= e[[l, 2]]]
, "NaN"];
Table[p, x, data]];
From the first example in the above referenced documentation:
data=1, 1, 2, 3, 6, 5, 8, 10, 4, 4;
edges=2, 4, 6, 8, 10;
disc[data,edges]
NaN,NaN,1,1,3,2,4,4,2,2
I'm sure there are more efficient/elegant solutions, and will revisit as time permits.
$endgroup$
add a comment |
$begingroup$
This is a very quick-n-dirty, but may serve as a simple example.
This creates a piecewise function following the first definition in Matlab's discretize documentation, then applies that to the data.
disc[data_, edges_] := Module[e = Partition[edges, 2, 1], p, l,
l = Length@e;
p=Piecewise[Append[Table[i, e[[i, 1]] <= x < e[[i, 2]], i, l - 1]
, l,e[[l, 1]] <= x <= e[[l, 2]]]
, "NaN"];
Table[p, x, data]];
From the first example in the above referenced documentation:
data=1, 1, 2, 3, 6, 5, 8, 10, 4, 4;
edges=2, 4, 6, 8, 10;
disc[data,edges]
NaN,NaN,1,1,3,2,4,4,2,2
I'm sure there are more efficient/elegant solutions, and will revisit as time permits.
$endgroup$
This is a very quick-n-dirty, but may serve as a simple example.
This creates a piecewise function following the first definition in Matlab's discretize documentation, then applies that to the data.
disc[data_, edges_] := Module[e = Partition[edges, 2, 1], p, l,
l = Length@e;
p=Piecewise[Append[Table[i, e[[i, 1]] <= x < e[[i, 2]], i, l - 1]
, l,e[[l, 1]] <= x <= e[[l, 2]]]
, "NaN"];
Table[p, x, data]];
From the first example in the above referenced documentation:
data=1, 1, 2, 3, 6, 5, 8, 10, 4, 4;
edges=2, 4, 6, 8, 10;
disc[data,edges]
NaN,NaN,1,1,3,2,4,4,2,2
I'm sure there are more efficient/elegant solutions, and will revisit as time permits.
edited Apr 9 at 8:03
answered Apr 9 at 0:38
ciaociao
17.5k138109
17.5k138109
add a comment |
add a comment |
$begingroup$
You may also use Interpolation
with InterpolationOrder -> 0
. However, employing Nearest
as Carl Woll did will usually be much faster.
First, we prepare the interplating function.
m = 20;
binboundaries = Join[-1., Sort[RandomReal[-1, 1, m - 1]], 1.];
f = Interpolation[Transpose[binboundaries, Range[0, m]], InterpolationOrder -> 0];
Now you can apply it to lists of values as follows:
vals = RandomReal[-1, 1, 1000];
Round[f[vals]]
$endgroup$
add a comment |
$begingroup$
You may also use Interpolation
with InterpolationOrder -> 0
. However, employing Nearest
as Carl Woll did will usually be much faster.
First, we prepare the interplating function.
m = 20;
binboundaries = Join[-1., Sort[RandomReal[-1, 1, m - 1]], 1.];
f = Interpolation[Transpose[binboundaries, Range[0, m]], InterpolationOrder -> 0];
Now you can apply it to lists of values as follows:
vals = RandomReal[-1, 1, 1000];
Round[f[vals]]
$endgroup$
add a comment |
$begingroup$
You may also use Interpolation
with InterpolationOrder -> 0
. However, employing Nearest
as Carl Woll did will usually be much faster.
First, we prepare the interplating function.
m = 20;
binboundaries = Join[-1., Sort[RandomReal[-1, 1, m - 1]], 1.];
f = Interpolation[Transpose[binboundaries, Range[0, m]], InterpolationOrder -> 0];
Now you can apply it to lists of values as follows:
vals = RandomReal[-1, 1, 1000];
Round[f[vals]]
$endgroup$
You may also use Interpolation
with InterpolationOrder -> 0
. However, employing Nearest
as Carl Woll did will usually be much faster.
First, we prepare the interplating function.
m = 20;
binboundaries = Join[-1., Sort[RandomReal[-1, 1, m - 1]], 1.];
f = Interpolation[Transpose[binboundaries, Range[0, m]], InterpolationOrder -> 0];
Now you can apply it to lists of values as follows:
vals = RandomReal[-1, 1, 1000];
Round[f[vals]]
answered Apr 9 at 6:05
Henrik SchumacherHenrik Schumacher
60.3k583169
60.3k583169
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194844%2fmap-list-to-bin-numbers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
HistogramList
seems similar. This could also be done efficiently withGroupBy
and some easy littleCompile
-d selection determiner. Or maybe hit it first withSort
then write something that only checks the next bin up. Again, can be easilyCompile
-d.$endgroup$
– b3m2a1
Apr 8 at 23:07
$begingroup$
I need it to work like a map (in terms of the order of the items in the resulting list). Of course it is possible to write something ...
$endgroup$
– Alan
Apr 9 at 0:13
$begingroup$
Related: 140577
$endgroup$
– Carl Woll
Apr 9 at 3:50
1
$begingroup$
Did you try
BinCounts
? I guess it is what you need.$endgroup$
– Rom38
Apr 9 at 4:52
$begingroup$
@Rom38 You probably meant
BinLists
, right?$endgroup$
– Henrik Schumacher
Apr 9 at 5:55