Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a matrix:

A = {{a, b}, {c, d}}

But each its element, itself, is another matrix too.

a={{0,1,1},{0,1,0}}
b={{0,0,1},{1,0,0}}

c={{0,0,0},{0,1,0}}
d={{0,0,1},{1,0,1}}

I want to comprise 'a' and 'b' with their elements; and respectively 'c' and 'd'. If just one element be similar in 'a' and 'b' and simultaneously equal to '1', (here:)

a13=b13=1

the 'b' element of 'A' must be replaced with zero. (here A12=0).

Question1:
For the comparison I write bellow line, I Do not know which must be written in the A['indexes referring to location of b'] in this line:

Do[If[a[[i, j]] == b[[k, m]], A['indexes referring to location of b'] = 0], {i, 1, 2}, {j, 1,3}, {k, 1, 2}, {m, 1, 3}]

Question2: All of us know each element of a matrix has two indexes {i and j; for example Aij}. are there any possibility to devote four indexes to a one element of a Matrix(for example Aijkm? Actually, if we can devote four indexes to each A element in the above 'Do' loop, the line will be written more easily.

Question3: if instead of 'a','b', 'c' and 'd' we have numerics:

A = {{1, 2}, {3, 4}}

Are the any possibility to devote a matrix to numeric!!!!!!?

For example

1={{0,1,1},{0,1,0}}
2={{0,0,1},{1,0,0}}

!!

share|improve this question

3 Answers 3

up vote 3 down vote accepted

You can subtract those matrices. I've replaced 0s with 2 in the second so 0 - 2 won't give 0.

If[
   FreeQ[# - (#2 /. (0) -> 2), 0, {2}],
   {##},
   {#, 0}
   ] & @@@ A
{
   {{{0, 1, 1}, {0, 1, 0}}, 0}, 
   {{{0, 0, 0}, {0, 1, 0}}, {{0, 0, 1}, {1, 0, 1}}}
  }

For the second part if those matrice are associated with numbers:

B = MapThread[List, {A, {{1, 2}, {3, 4}}}, 2]

enter image description here

If[
   FreeQ[#[[ 1]] - (#2[[ 1]] /. (0) -> 2), 0, {2}],
   {##}[[ ;; , 2]],
   {#[[2]], 0}
   ] & @@@ B
{{1, 0}, {3, 4}}
share|improve this answer
    
Thank you so much for your answer. But Where is 'a'. As it shown, 'b' remained and 'a' eliminated. I wanted the vise versa process –  mostafa May 30 at 6:43
    
No problem, But the major one in your answer is: I could not make the matrix: {{a, 0}, {c, d}} in the result of your code!! –  mostafa May 30 at 6:47
    
The result of the second one as you write, is {{1}, {3, 4}} How can I get a Zero besides '1'? I mean, the result become same as: {{1, 0}, {3, 4}} –  mostafa May 30 at 7:01
    
@mostafa done. please, hold on with an accept a day or two and accept the answer which fits you needs the best :) Early accept may discourage others. –  Kuba May 30 at 7:31
    
Kuba,I read some Mathematica documentation but I confused in translation of this line: If[FreeQ[#-(#2/.(0)→2),0,{2}],{##},{#,0}]&"@@@" A I think I could not deeply understand what happens in this line completely, because I am not able to generalize this process to a 3*3 matrix in which must be a possibility for comparison A13 with A11. –  mostafa May 31 at 16:31

The question as it stands is nearly indecipherable. Please, make an effort to better explain what you're trying to do, with complete examples, e.g., of inputs and expected outputs at least.

The question itself should not be a puzzle to readers...

That said, I think this is what your asking for (if not, comment, I'll happily delete).

Part 1:

"In my 2x2 matrix, where each element is itself a 2X3 matrix of zeroes and ones, compare the elements in each row of the 2X2 matrix, and if the second element has a one corresponding to the same position in the first element, make it zero":

a = {{0, 1, 1}, {0, 1, 0}}
b = {{0, 0, 1}, {1, 0, 0}}

c = {{0, 0, 0}, {0, 1, 0}}
d = {{0, 0, 1}, {1, 0, 1}}

aa = {{a, b}, {c, d}}

aa[[All, 2]] = BitXor[aa[[All, 2]], BitAnd @@@ aa];

aa

(*
{{{{0, 1, 1}, {0, 1, 0}}, {{0, 0, 0}, {1, 0, 0}}}, 
 {{{0, 0, 0}, {0, 1, 0}}, {{0, 0, 1}, {1, 0, 1}}}}
*)

Note I used aa for the full matrix instead of A. It is generally a bad idea to use uppercase symbols/initials: you might interfere with a built-in symbol, leading to at the very least confusion.

Second part:

Of course. Read the documentatation. E.g., here's one 4 indices "deep":

fourDims = Array[#1*#2*#3*#4 &, {2, 3, 2, 2}]
Depth@fourDims
fourDims[[2, 3, 1, 1]]

(*

{{{{1, 2}, {2, 4}}, {{2, 4}, {4, 8}}, {{3, 6}, {6, 12}}}, 
 {{{2, 4}, {4, 8}}, {{4, 8}, {8, 16}}, {{6, 12}, {12, 24}}}}

6

*)

Part 3: Yes, you can use, for example, rules to accomplish this:

aa = {{1, 2}, {3, 4}}
aa /. {1 -> a, 2 -> b, 3 -> c, 4 -> d}

(*

{{1, 2}, {3, 4}}

{{{{0,1,1},{0,1,0}},{{0,0,1},{1,0,0}}},{{{0,0,0},{0,1,0}},{{0,0,1},{1,0,1}}}}

*)

Note the end result is precisely the same as the definition of aa above. The second list simply applies rules to transform the numeric entries into the desired components a,b,c... Read the documentation re: Rules for details.

share|improve this answer
    
Thank you so much for your comments and answers. OK, next I will try to explain more obviously. This example is near to the main question with which I am challenging. I am trying with your help and other skillful persons here to solve it step by step. thanks a bunch. –  mostafa May 30 at 7:11
a = {{0, 1, 1}, {0, 1, 0}}
b = {{0, 0, 1}, {1, 0, 0}}
c = {{0, 0, 0}, {0, 1, 0}}
d = {{0, 0, 1}, {1, 0, 1}}

aA = {{a, b}, {c, d}}


If[Or @@ Join @@ MapThread[#1 == #2 != 0 &, {##}, 2], {#, 0}, {##}] & @@@ aA 

or

aA[[All, 2]] = If[Or @@ Join @@ MapThread[#1 == #2 != 0 &, {##}, 2], 0, #] & @@@ aA; aA

both give

(* {{{{0, 1, 1}, {0, 1, 0}},   0}, 
     {{{0, 0, 0}, {0, 1, 0}}, {{0, 0, 1}, {1, 0, 1}}}} *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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