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.

Supose I have matrices a b and c like

   a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
   b = {7, 8, 9};
   c = {{1},{4},{7}};

I want to get {{1, 2, 3}, {4, 5, 6}} from a and b, also {{2,3},{5,6},{8,9}} from a and c ? In other words, I want to delete the part which is the same as b or c from a.

share|improve this question
    
Could you clarify: Do you wish to delete elements or only entire matching rows/columns? What do you expect to get for your example in a comment, a = IdentityMatrix[3]; b = {{1, 0}, {0, 1}, {0, 0}}? –  Michael E2 Mar 31 '13 at 13:09
add comment

1 Answer

Try to use Complement!

Complement[a,{b}]

{{1, 2, 3}, {4, 5, 6}}

Or use DeleteCases[a, b]. Also Cases[a, Except@b] works!

For the list c use

MapThread[Complement, {a, c}]
share|improve this answer
    
Complement works fine. Thanks a lot! –  novice Feb 17 '13 at 11:48
    
Wait, it has a problem when dealing with A = IdentityMatrix[3]; B = {{1, 0}, {0, 1}, {0, 0}} Complement[A, B] DeleteCases[A, B] Cases[A, Except@B] I need the last column of A, but the result does match. –  novice Feb 17 '13 at 12:02
1  
The solution works when Length of rows of A is equal to the length of B. To consider other cases you need to reformulate your question! –  PlatoManiac Feb 17 '13 at 12:07
    
I supplement another case. Complement fails this time. –  novice Feb 17 '13 at 12:20
    
Thanks, I am a novice, MapThread[Complement[#1, #2] &, {a, c}] works, although I don't quite understand the grammar. –  novice Feb 17 '13 at 13:23
show 2 more comments

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.