10

Suppose I have two Lists (Not equal length). Both contain random numbers between 0 to 5000 (or another max limit):

A= {1000, 450, 50, 4100, ...,670}

B={500, 10, 4561, 2000, ...}

I would like to take each number from List A and compare it to all numbers in List B.

If the absolute value of B is smaller than A I will count it. If not I will not count it.

For example, in the Lists above, we start with 1000 in List A and compare it to all numbers in B, 500<1000, 10<1000... I will count all numbers that are smaller than 1000 and Build a new List C that contains integers which represents the amount of all numbers that were smaller than 1000 (here only 2), 450 (only 1), 50 (0), 4100 (3)...

In the next step I return the same procedure for 450 etc.

So C={2,1,0,3….}

Can someone help me and show me a way?

user64494
  • 26,149
  • 4
  • 27
  • 56
Danny
  • 201
  • 1
  • 6
  • Map[Count[list2, x_ /; x < #] &, list1] should get you started. If lists are huge, there are more efficient ways... – ciao May 12 '16 at 07:24
  • @Danny - all your questions seem very similar: making listC from listA and listB – Jason B. May 12 '16 at 07:51

6 Answers6

11

Between Jason's and ciao's in terms of performance:

Length[listB] - Total[UnitStep[listB - # & /@ listA], {2}]
Kuba
  • 136,707
  • 13
  • 279
  • 740
8
listA = RandomInteger[1000, 10]
listB = RandomInteger[1000, 10]

Out[112]= {33, 651, 45, 947, 743, 964, 292, 182, 468, 563}

Out[113]= {739, 127, 687, 104, 840, 990, 475, 455, 4, 878}

First@First@Position[Sort@Join[{#}, listB], #] - 1 & /@ listA

Out[116]= {1, 5, 1, 9, 7, 9, 3, 3, 4, 5}

Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • Try these with large lists (say 50K elements each)... OP perhaps might specify if performance matters or if lists are beyond trivial sizes... – ciao May 12 '16 at 08:15
  • @ciao I did, and the Count and LengthWhile methods are drastically slower. I'll post the timing if you think it's useful – Jason B. May 12 '16 at 08:21
  • @JasonB:Could you tell how to run this code using Wolfram Mathematica Online.I get this " « Graphics ‘ Animation ‘ " is incomplete; more input is needed while evaluating the code. – justin May 12 '16 at 08:26
  • hmmm...... I would remove that first line, « Graphics‘Animation‘, then I would replace the last line with ListAnimate[film] - never used MMA online, but it works in the desktop version – Jason B. May 12 '16 at 08:30
  • I meant these will all be slow... – ciao May 12 '16 at 08:34
  • @JasonB:Sorry but it's saying the peak memory limit of computer has reached while using Mathematica online. – justin May 12 '16 at 08:43
7

Here's a quck-and-dirty for lists of non-trivial size. There are some other ways perhaps faster, but more details on list composition would be useful before I spend further time.

With[{u = Union@#1}, 
   Replace[#1,AssociationThread[u -> Ordering[Ordering[Join[u, #2]]][[;; Length@u]] - 
                                Range@Length@u], 1]] &[list1, list2]

Results of a quick benchmark, taking successively longer slices of two 10K long lists (usual loungebook performance caveats...):

enter image description here

ciao
  • 25,774
  • 2
  • 58
  • 139
2

Using Jason's data

a = {33, 651, 45, 947, 743, 964, 292, 182, 468, 563};
b = {739, 127, 687, 104, 840, 990, 475, 455, 4, 878};

Count[b, x_ /; x < #] & /@ a

{1, 5, 1, 9, 7, 9, 3, 3, 4, 5}

eldo
  • 67,911
  • 5
  • 60
  • 168
2

Using Jason's data:

a = {33, 651, 45, 947, 743, 964, 292, 182, 468, 563};
b = {739, 127, 687, 104, 840, 990, 475, 455, 4, 878};

Using Compile:

cf = Compile[{{a, _Integer, 1}, {b, _Integer, 1}}, 
     Module[{result = ConstantArray[0, Length[a]]}, 
     Do[result[[i]] = Total[UnitStep[a[[i]] - b]], {i, Length[a]}];
     result], CompilationTarget -> "C", RuntimeOptions -> "Speed"]

cf[a, b] // RepeatedTiming

({6.5743410^-6, {1, 5, 1, 9, 7, 9, 3, 3, 4, 5}}*)

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
1

An attempt was made at an answer like this, but it was wrong, deleted and I cannot comment to show the right way. So, I am adding it as an answer.

I am grabbing data from @Jason B.

a = {33, 651, 45, 947, 743, 964, 292, 182, 468, 563};
b = {739, 127, 687, 104, 840, 990, 475, 455, 4, 878};

and now

Count[False] /@ Outer[Less, a, b]

gives

{1, 5, 1, 9, 7, 9, 3, 3, 4, 5}

and of course, equivalently, we have

Count[True] /@ Outer[Greater, a, b]
bmf
  • 15,157
  • 2
  • 26
  • 63