2
$\begingroup$

We have two nested matrix and a code for replacement following an equality as:

m1 = {{{1, 2, 2, I}, {1, 1, -2, 1}, {3, 2, 2, -I}, {1, 2, 
 0, -I}}, {{3, 4, 4, I}, {1, 1, 4, 1}, {3, 3, 4, -1}, {1, 1, 2, 
 I}}};
m2 = {{{1, 0, 2, 1}, {3, 1, -2, 4}, {3, 4, 4, 0}, {1, 0, 0, 2}}, {{1, 
 4, 4, I}, {1, 2, 2, 3}, {0, 3, 4, -1}, {1, 2, 1, 1}}};

m3 must be constructed by code:

m3 = ConstantArray[0, {2, 4, 4}];
Do[
   Do[
     Do[

        Do[

     If[m1[[k, i]][[1 ;; 3]] == m2[[r, s]][[1 ;; 3]], 
     m3[[k, i]] = m2[[r, s]], m3[[k, i]] = m1[[k, i]]]

         , {s, 1, 4}]


      , {r, 1, 2}],

   {i, 1, 4}],
{k, 1, 2}];

But because an iteration changes the condition of comparison of m1[[k, i]] I could not obtained the desired result which has to be as

enter image description here

As a matter of fact, the above code just bring m1 again! How can I remove this problem and shorten the code instead of doing four iterations.?

$\endgroup$
  • $\begingroup$ What if you have multiple {1, 2, 2, x} elements in m2? Each with different x. $\endgroup$ – Kuba Jan 22 '16 at 8:35
  • $\begingroup$ Good question, however this case could not be happened in the above example but it must exit the comparison. I mean the first one is important. the first meeting satisfies the comparison. $\endgroup$ – Unbelievable Jan 22 '16 at 8:39
  • $\begingroup$ What is the first meeting depends of the way you iterate. And if you want different methods, you have to say exactly, what does it mean to be earlier in 2D matrix. $\endgroup$ – Kuba Jan 22 '16 at 8:42
  • $\begingroup$ You are completely right. But the first meeting is being face to face of a sub_list in m1 to a sub_list of m2 and if the equality is ok the iteration must go to another sub_list of m1 and start the next comparison and repalcement process $\endgroup$ – Unbelievable Jan 22 '16 at 8:52
  • $\begingroup$ element #[[1,2]] will be before #[[2,1]] only if the outer iteration goes through rows and the inner on through columns. If you just replace r with k in table spec it will be different. That's what I was talking about. $\endgroup$ – Kuba Jan 22 '16 at 8:56
5
$\begingroup$
m3 = m1 /. ({#, #2, #3, _} -> {##} & @@@ Flatten[m2, 1])
{
 {{1, 2, 2, 3}, {1, 1, -2, 1}, {3, 2, 2, -I}, {1, 2, 0, -I}}, 
 {{3, 4, 4, 0}, {1, 1, 4, 1}, {3, 3, 4, -1}, {1, 1, 2, I}}
}

So basically we create replacement rules from m2, e.g. from {1,0,2,1} we get {1,0,2,_} -> {1,0,2,1}. Then we replace it in m1, if {a,b,c,_} matches, those 3 first elements you wanted, then the replacement is done, the rest of m1 stays as it was.


Edit

As far as I remember your sublists are of length 8, then it may be more convenient to slightly modify approach to not type # #2... manually:

m3 = m1 /. (ReplacePart[#, -1 -> _] -> # & /@ Flatten[m2, 1])
| improve this answer | |
$\endgroup$
  • $\begingroup$ I could understand what you said and deleted my comments. So much thanks for your answers and comments. $\endgroup$ – Unbelievable Jan 22 '16 at 18:14
  • $\begingroup$ Nicely and wonderful!!! $\endgroup$ – Unbelievable Jan 22 '16 at 18:19

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.