0
$\begingroup$

I have two (nxn)-matrices A=(a_{ij}) and B=(b_{ij}) and I'd like to create the (nxnxnxn)-List

C=(b_{ij}*A)

so e.g.

SeedRandom["testing"]
bb = ConstantArray[1, {3, 3}];
aa = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Table[
 bb[[i, j]]*aa, {i, 1, 3, 1}, {j, 1, 3, 1}]

I of course can construct it via Table[], see the above code. but it is very slow when we talk about 100x100. I guess theres a trick/possibility via Map[], but it is not

SeedRandom["testing"]
aa = RandomReal[1, {3, 3}]
bb = ConstantArray[1, {3, 3}];
cc = Map[aa, bb]
cc // MatrixForm

Thanks. And I guess a similar question came up, again matrices and vectors

vv = {1, 2, 3}
vvM = Table[
  vv[[i]] - vv[[j]], {i, 1, 3, 1}, {j, 1, 3, 1}]
vvM // MatrixForm

Thanks for all the help. Just for information, I think I know Mathematica okayish but when it comes to @, @@, #, stuff like that, I am pretty new to that

$\endgroup$
5
  • $\begingroup$ I do not understand your description. Can you please be more explicit about what is the expected value of element $(i,j,k)$ of the resulting matrix $C$? From what you say, $a_{ij}\times B$ is a $n\times n$ matrix for every $(i,j)$, and so you described $C$ is a $n\times n\times n\times n$ matrix (rank-4), not rank-3. $\endgroup$
    – Roman
    Nov 22, 2020 at 11:14
  • $\begingroup$ KroneckerProduct? $\endgroup$
    – cvgmt
    Nov 22, 2020 at 11:16
  • $\begingroup$ Can you please show how you do it with Table and we will help you to speed it up. At the moment your question is unclear. $\endgroup$
    – yarchik
    Nov 22, 2020 at 17:52
  • $\begingroup$ dear Roman and Yarchik, you're totally correct, there's an error. I'll edit the posting and add a similar question, thank you in advance $\endgroup$ Nov 23, 2020 at 9:26
  • 1
    $\begingroup$ Outer[Times, bb, aa] $\endgroup$
    – cvgmt
    Nov 23, 2020 at 10:12

1 Answer 1

2
$\begingroup$

Maybe your need Outer.

bb = ConstantArray[1, {3, 3}];
aa = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Outer[Times, bb, aa]
vv = {1, 2, 3}
Outer[Subtract, vv, vv]
$\endgroup$
1
  • $\begingroup$ perfect, thanks! $\endgroup$ Nov 26, 2020 at 15:23

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.