2
$\begingroup$

I have the following code:

k12 = {{6.08, 1.52, -6.08, -1.52},{1.52, 380448.03, -1.52, -380448.03}, {-6.08, -1.52, 6.08, 1.52},{-1.52, -380448.03, 1.52, 380448.03}}

k14 = ({{3.84, 2.56, -3.84, -2.56},{2.56, 1.70, -2.56, -1.70},{-3.84, -2.56, 3.84, 2.56},{-2.56, -1.70, 2.56, 1.70}})

kglobal = ({{k12[[1, 1]], k12[[2, 1]], k12[[3, 1]], k12[[4, 1]], 0, 0, 0, 0},{k12[[1, 2]], k12[[2, 2]], k12[[3, 2]], k12[[4, 2]], 0, 0, 0, 0},{k12[[1, 3]], k12[[2, 3]], k12[[3, 3]], k12[[4, 3]], 0, 0, 0, 0},{k12[[1, 4]], k12[[2, 4]], k12[[3, 4]], k12[[4, 4]], 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0}}) + ({{k14[[1, 1]], k14[[2, 1]], 0, 0, 0, 0, k14[[3, 1]], k14[[4, 1]]},{k14[[1, 2]], k14[[2, 2]], 0, 0, 0, 0, k14[[3, 2]], k14[[4, 2]]},{0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0},{k14[[1, 3]], k14[[2, 3]], 0, 0, 0, 0, k14[[3, 3]], k14[[4, 3]]},{k14[[1, 4]], k14[[2, 4]], 0, 0, 0, 0, k14[[3, 4]], k14[[4, 4]]}})

enter image description here

The value k12[[1.1]] should go to kglobal[[1.1]] and so forth until k12[[4.4]] be kglobal[[4.4]].

However the value k14[[1.1]] must be added to the value already exists for k12[[1.1]] modifying kglobal[[1.1]].

Another problem is that the value of k14[[3, 1]] should go to kglobal[[7.1]] and not for kglobal[[3.1]], that is, there is a breach of sequences.

How can I create this kind of association?

Note that for any result I needed to write the matrices manually, so nothing automated.

I was thinking in to use Insert, Association, or another command that I could automate this task.

$\endgroup$
3
$\begingroup$

I think try to create a zero-matrix and add your matrix on it will be helpful, just like the following code shows.

In[22]:= kglobal2 = Module[{sum = ConstantArray[0, {8, 8}]},
  sum[[1 ;; 4, 1 ;; 4]] += k12;
  sum[[{1, 2, -2, -1}, {1, 2, -2, -1}]] += k14\[Transpose];
  sum
  ]

Out[22]= {{9.92, 4.08, -6.08, -1.52, 0, 0, -3.84, -2.56}, {4.08, 
  380450., -1.52, -380448., 0, 0, -2.56, -1.7}, {-6.08, -1.52, 6.08, 
  1.52, 0, 0, 0, 0}, {-1.52, -380448., 1.52, 380448., 0, 0, 0, 0}, {0,
   0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {-3.84, -2.56, 0, 
  0, 0, 0, 3.84, 2.56}, {-2.56, -1.7, 0, 0, 0, 0, 2.56, 1.7}}

In[23]:= kglobal2 == kglobal

Out[23]= True
$\endgroup$
  • $\begingroup$ This is exactly what I needed. Because I had not idea how could I enter the values in the correct position. With sum[[1 ;; 4, 1 ;; 4]] += k12 and sum[[{1, 2, -2, -1}, {1, 2, -2, -1}]] += k14 I can do this easily. $\endgroup$ – LCarvalho Jul 24 '16 at 11:58
2
$\begingroup$

Will this a-bit-complex code fullfill your need?

arrayinsert[mat_, x_, y_] := 
 With[{pre = ArrayFlatten@List@Riffle[#, Unevaluated@ConstantArray[0, {Length@#[[1]], x}]] & /@ mat}, 
  ArrayFlatten[List /@ Riffle[pre, Unevaluated@ConstantArray[0, {y, Length@pre[[1, 1]]}]]]]

ArrayPad[k12, {0, 4}] + arrayinsert[Partition[k14, {2, 2}], 4, 4]

The key of this piece of code is the function arrayinsert, check the following code and figure and you'll realize how to use this function:

arrayinsert[Partition[ConstantArray[1, {9, 9}], {3, 3}], 2, 2] // ArrayPlot

arrayinsert[Transpose[Internal`PartitionRagged[Transpose@#, Range@4] & /@ 
    Internal`PartitionRagged[ConstantArray[1, {10, 10}], Range@4]], 2, 4] // ArrayPlot

illus

$\endgroup$
  • $\begingroup$ His answer was incredible. It was like a spell. But for my level of knowledge I am not prepared to this. $\endgroup$ – LCarvalho Jul 24 '16 at 12:06
  • $\begingroup$ @LeandroMacieldeCarvalho Thanks for your appreciation! I'm glad that I can help you. :) $\endgroup$ – Wjx Jul 24 '16 at 13:50

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.