# List multiplication

I have a the following code:

data = RandomReal[1, {13000, 3}];

x = 1;
y = 2.3;
z = 0.7;

data[[All, 1]] = data[[All, 1]]*x;
data[[All, 2]] = data[[All, 2]]*y;
data[[All, 3]] = data[[All, 3]]*z;


How would you solve the recalculation of data?

• What do you mean by "solve the recalculation"? – swish Oct 11 '17 at 13:35
• Maybe, you would like to know about TimesBy (*=)? Example: data[[All, 1]] *= x; – Henrik Schumacher Oct 11 '17 at 13:40

Convert your vector to a diagonal matrix, and then use Dot:

data = RandomReal[1,{13000,3}];
data1 = data . DiagonalMatrix[{x, y, z}]; //RepeatedTiming


{0.000051, Null}

Another very fast approach suggested by @LLlAMnYP (making sure to pack the vector first):

m = DeveloperToPackedArray @ N @ {x, y, z};
data2 = Transpose[data] m //Transpose; //RepeatedTiming


{0.000085, Null}

data[[All, 1]] = data[[All, 1]] x;
data[[All, 2]] = data[[All, 2]] y;
data[[All, 3]] = data[[All, 3]] z;

data == data1 == data2


True

• Transpose[data] {x,y,z} // Transpose is competitive too, especially if instead of {x,y,z} we have a packed array. – LLlAMnYP Oct 11 '17 at 14:11
• Thank you and also to @LLlAMnYP for the solutions ... great. – lio Oct 11 '17 at 14:47

Maybe you would like to get to know TimesBy, a.k.a. *=.

data0 = RandomReal[1, {1300000, 3}];
x = 1;
y = 2.3;
z = 0.7;


The original approach:

data = data0;
AbsoluteTiming[
data[[All, 1]] = data[[All, 1]]*x;
data[[All, 2]] = data[[All, 2]]*y;
data[[All, 3]] = data[[All, 3]]*z;
]
data1 = data;

(* {0.097083, Null} *)


A naive approach with TimesBy:

data = data0;
AbsoluteTiming[
data[[All, 1]] *= x;
data[[All, 2]] *= y;
data[[All, 3]] *= z;
]
data2 = data;

(* {0.104508, Null} *)


Seemingly, this does not help. Hence another approach with TimesBy:

data = data0;
AbsoluteTiming[
data *= ConstantArray[DeveloperToPackedArray[N[{x, y, z}]], Length[data]];
]
data3 = data;

(* {0.021616, Null} *)


... and a check for correct results:

data1 == data2
data1 == data3

(* True *)
(* True *)

• Thank you vey much for you help. – lio Oct 11 '17 at 16:52