1
$\begingroup$

New to Mathematica. I want to create and combine K1, K2, and K3 (shown in the first picture below) into the 8x8 matrix shown in the second picture below and keep the heading as shown if possible:

The three K matrices

The final matrix

Elements indicated by the same headings from each K matrix should be added together and placed at the same position in the 8x8 matrix. For example, here we have 3 elements at the position (u1,u1); one from each K matrix. These elements are 0, 0.5, and 1 and each of them is multiplied by the constant outside the matrix. Thus, they should be added together to form element (u1,u1), which is 1.354 in the 8x8 matrix. Similarly, we have one element indicated by (v2,v2), which is 1, in the matrix K1. So, that element should be at the position (v2,v2) in the 8x8 matrix. How do I do that in Mathematica?

I could define each K matrix as an 8x8 matrix with zeros as fillers, but sometimes I need to deal with bigger matrices and it gets tedious.

$\endgroup$
2
  • 1
    $\begingroup$ Please paste Mathematica code that you have experimented with so far. Pasting pictures in not useful as no one is likely to enter this data on their own; it is time consuming and prone to error. At this point, it is not clear, for instance, how the three matrices will combine to generate an 8x8 matrix. $\endgroup$
    – Syed
    Oct 14, 2021 at 18:43
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – LouisB
    Oct 14, 2021 at 19:12

1 Answer 1

2
$\begingroup$

Here is an approach with Association (and Dataset for visualisation) :

vars1 = {"u1", "v1", "u2", "v2"};
vars2 = {"u1", "v1", "u3", "v3"};
vars3 = {"u1", "v1", "u4", "v4"};

k1 = (2 10^11 6 10^-4)/3 {{0, 0, 0, 0}, {0, 1, 0, -1}, {0, 0, 0, 0}, {0, -1, 0, 1}};

k2 = (2 10^11 6 10^-4)/(3 Sqrt[2]) {{0.5, 0.5, -0.5, -0.5}, {0.5, 
     0.5, -0.5, -0.5}, {-0.5, -0.5, 0.5, 0.5}, {-0.5, -0.5, 0.5, 0.5}};

k3 = (2 10^11 6 10^-4)/3 {{1, 0, -1, 0}, {0, 0, 0, 0}, {-1, 0, 1, 0}, {0, 0, 0, 0}};

as1 = Map[AssociationThread[vars1 -> #] &, k1, {0, 1}];
as1 // Dataset

as2 = Map[AssociationThread[vars2 -> #] &, k2, {0, 1}];
as2 // Dataset

as3 = Map[AssociationThread[vars3 -> #] &, k3, {0, 1}];
as3 // Dataset

enter image description here

Merge[{as1, as2, as3}, Merge[#, Total[#]/(4 10^7) &] &] // Dataset  

enter image description here

$\endgroup$
2
  • $\begingroup$ I realize only now that it's difficult to retrieve a normal array (a rectangular List of List) from the Dataset above. I will try to see that tomorrow. $\endgroup$
    – andre314
    Oct 14, 2021 at 22:12
  • $\begingroup$ This is awfull but does the job : Merge[{as1, as2, as3}, Merge[#, Total[#]/(4 10^7) &] &] // With[{keys = Keys[#]}, # //Query[keys /* Values , keys /* Values ]] & // (# /. Missing[___] -> 0. &) $\endgroup$
    – andre314
    Oct 14, 2021 at 22:26

Your Answer

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