1
$\begingroup$

Hi i have a list of n elements and i want to know the 30 maxim values of list without changing his order. And suppose that i have a second list with n elements and i want to add this elements to the previous list but just to the 30 ones that have the maximum values, and also without changing his order. How i can do this? This is the code that i use to add elements to other list but i don´t know how to do it in the way that i explain before.

plotData = Partition[Riffle[informacion1, desvi], 2]

Any suggestions?

$\endgroup$
  • 2
    $\begingroup$ Check out Ordering in the docs. $\endgroup$ – N.J.Evans Sep 17 '15 at 2:23
4
$\begingroup$

To find the 30 largest of your first list, we'll call it list1, use

TakeLargest[lis1,30]

To then add only to these largest from the second list, let's call it list2, we can use Ordering to identify the positions where the 30 largest are.

list3=list1;
(list3[[#]] = list1[[#]] + list2[[#]]) &@Ordering[list1, -30]

Then list3 will contain a copy of list1 with only the desired elements changed.


Edit: A small demo.

list1 = RandomInteger[1000, 10]
list2 = RandomInteger[1000, 10]
list3 = list1;
(list3[[#]] = list1[[#]] + list2[[#]]) &@Ordering[list1, -4];
list3

Sort[list1]
Sort[list3]

{383, 226, 192, 397, 565, 60, 184, 535, 423, 77} {667, 297, 386, 866, 625, 881, 233, 327, 288, 411} {383, 226, 192, 1263, 1190, 60, 184, 862, 711, 77}

{60, 77, 184, 192, 226, 383, 397, 423, 535, 565} {60, 77, 184, 192, 226, 383, 711, 862, 1190, 1263}

Notice only the 4 largest in list1 see their values change in list3, while the order is retained.

$\endgroup$
  • 1
    $\begingroup$ Or (list3[[#]] += list2[[#]]) &@Ordering[list1, -4]; $\endgroup$ – Dr. belisarius Sep 17 '15 at 7:19
  • 1
    $\begingroup$ @belisarius Ah yes, the compound operator. If only Matlab hadn't broken me of expecting such a convenience in computational software, I'd be much better at remembering it's available in Mathematica. D= $\endgroup$ – IPoiler Sep 17 '15 at 8:06
1
$\begingroup$

As N.J.Evans hinted you can do the first part with Ordering. I'll take the largest seven instead of 30 for brevity, and use n = 20. First generate sample data:

n = 20;
p = 7;
SeedRandom[0]
list = RandomInteger[99, 20]
{83, 66, 4, 21, 71, 67, 16, 67, 76, 28, 21, 43, 17, 46, 53, 84, 85, 17, 50, 15}

Find the places of the largest seven values:

m = Ordering[list, -p]
{6, 8, 5, 9, 1, 16, 17}

Sort that list of places to keep them in order:

m = Sort[m]
{1, 5, 6, 8, 9, 16, 17}

Extract the values from list:

list[[m]]
{83, 71, 67, 67, 76, 84, 85}

I am not sure I follow the second part of the question but I think this does what you want:

SeedRandom[1]
list2 = RandomInteger[99, 20]
{80, 14, 0, 67, 3, 65, 23, 97, 68, 74, 15, 24, 4, 90, 83, 70, 1, 30, 48, 25}
m2 = Sort @ Ordering[list2, -p]
{1, 8, 9, 10, 14, 15, 16}
list2[[m2]]
{80, 97, 68, 74, 90, 83, 70}
list3 = list;
list3[[m]] = {list3[[m]], list2[[m2]]}\[Transpose];

list3
{{83, 80}, 66, 4, 21, {71, 97}, {67, 68}, 16, {67, 74}, {76, 90},
  28, 21, 43, 17, 46, 53, {84, 83}, {85, 70}, 17, 50, 15}

Flatten if desired:

list3 // Flatten
{83, 80, 66, 4, 21, 71, 97, 67, 68, 16, 67, 74, 76, 90,
 28, 21, 43, 17, 46, 53, 84, 83, 85, 70, 17, 50, 15}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.