0
$\begingroup$

I have a list y={{a,b},{c,d},...} of dimension {10^5, 2}, and number x. I want to divide only the second part of each element of the list by x, and have as result a new list {{a,b/x},{c,d/x},...}. What is the fastest/best way to do it? This is what I have done:

{#[[1]], Divide[#[[2]], x]} & /@y

Thanks

$\endgroup$
  • $\begingroup$ List[[All,2]]=list[[All,2]]/x $\endgroup$ – Mike Honeychurch Jan 16 '15 at 9:50
  • $\begingroup$ y[[All, 2]] /= x; y $\endgroup$ – Kuba Jan 16 '15 at 10:20
  • 1
    $\begingroup$ Why are answers given as comments? $\endgroup$ – Philipp Jan 16 '15 at 10:38
  • $\begingroup$ @Philipp I was looking for a duplicate :) $\endgroup$ – Kuba Jan 16 '15 at 11:32
2
$\begingroup$

If y is a packed array then Dot should be quite fast:

y = RandomReal[{0, 1}, {10^5, 2}];
x = 12.3;

result = y.{{1, 0}, {0, 1/x}};

This takes about a millisecond on my not-very-fast PC.

| improve this answer | |
$\endgroup$

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