2
$\begingroup$

This is probably a simple question, I have matrix a , and I construct b from a by adding a variable to the first column. b is a function of variable d, and now I need to do mathematical calculation on the first and third column of b and save it as c. c is also a function of variable d.

a = Table[{RandomReal[{1, 4}], RandomReal[{1, 4}], 
RandomReal[{1, 4}]}, {i, 1, 5}];
b[d_] := MapAt[# + d &, A, {{All, 1}}];
c[d_] := Table[
 Sqrt[(b[d][[i]][[1]])^2 + (b[d][[i]][[3]])^2], {i, 1, 5}];

The way I did it works, but I am looking for a more efficient way to play with functions which are being used in other lines of my program (like of I need to have also functions g[d], k[d],...). Can anybody help me to do this type of calculation faster?

$\endgroup$
2
$\begingroup$
Clear[a, b, c, d, m];
a = Array[m, {5, 3}];
b = SubsetMap[# + d &, a, {All, 1}];
c = Sqrt[# . #] & /@ b[[All, {1, 3}]]

enter image description here

Or

a = Array[m, {5, 3}];
Sqrt[(#1 + d)^2 + #2^2] & @@@ a[[All, {1, 3}]]
$\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.