4
$\begingroup$

Okay, so let's say I have these lists and a number

a = {1, 2, 3};
b = {4, 5, 6};

And now I want to create 3 matrices that each look like this

M = {{n^2 - b^2, a*b}, {b*a, n^2 - a^2}}

My desired output is that M is a list of 3 matrices, where

M = {M[[1]], M[[2]], M[[3]]}

and

M[[i]] = {{n^2 - b[[i]]^2, a[[i]]*b[[i]]}, {b[[i]]*a[[i]], n^2 - a[[i]]^2}}

I know I could use Table to do this, but I guess there is a more elegant method. Thanks!

$\endgroup$
2
  • $\begingroup$ You have n[[i]] in your code. Is n also a list of numbers? $\endgroup$
    – march
    Commented Nov 21, 2023 at 21:02
  • $\begingroup$ Sorry, it's a typo. n should be a single number and n[[i]] makes no sense, it should be just n $\endgroup$
    – Seigel
    Commented Nov 21, 2023 at 21:16

2 Answers 2

4
$\begingroup$

One can do one of the three following, among others:

{{n^2 - #2^2, #1 #2}, {#2 #1, n^2 - #1^2}} & @@@ Transpose[{a, b}]

Table[{{n^2 - b[[i]]^2, a[[i]] b[[i]]}, {a[[i]] b[[i]], n^2 - a[[i]]^2}}, {i, 1, Length@a}]

MapThread[{{n^2 - #2^2, #1 #2}, {#2 #1, n^2 - #1^2}} &, {a, b}]
$\endgroup$
3
$\begingroup$
a = {1, 2, 3};
b = {4, 5, 6};
Transpose@MapThread[List, {{n^2 - b^2, a*b}, {b*a, n^2 - a^2}}, 2]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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