1

I'm used to MATLAB so this might be a stupid question but Mathematica is freaking me out.

I have a big matrix with the following format:

M = {{A,B,C},{1,{1,3,6,7},0},{1,{3,7,9},1}}

As you can see, each element in the second column is a separate list again. These separate lists contain integers, so it's no problem to get back to them. For example if I use MemberQ[M[[2,2]],1] it returns True, as it's supposed to do.

What I want to do now: my Matrix is a little longer, contains approx. 5'000 rows. I want to check every seperate list mentioned above for the appearance of a specific integer, as I did above with the MemberQ.

As a result it should give me something like the following (taking M as example): {True,False}.

I thought that's supposed to be easy with a for-loop as it is in MATLAB, but Mathematica always stops when condition is not met...

Looking forward to your answers. Please state if I need to clarify my question!

Phily
  • 171
  • 1
  • 1
  • 7
  • Please be precise in the problem description. The second row of this matrix is {1,{1,3,6,7},0}. The elements of the second row are not lists, contradicting what you said. memberQ[M[2,2],1] is not correct Mathematica code and won't return True. If you mean MemberQ[M[[2,2]],1], please write it as such. – Szabolcs Jun 14 '14 at 13:21
  • Sorry for that, recognized it myself, already edited. Meant second column, and memberQ now correct. – Phily Jun 14 '14 at 13:22
  • So B is a list then? – Szabolcs Jun 14 '14 at 13:22
  • Not a Mma matrix (see MatrixQ) ... but a tensor. – wolfies Jun 14 '14 at 13:23
  • Is MemberQ[#[[2]], 1] & /@ M what you are looking for? – Öskå Jun 14 '14 at 13:24
  • You can try MemberQ[#, 1] & /@ M[[All, 2]]. – Szabolcs Jun 14 '14 at 13:24
  • @Öskå When I looked at the question, it was not the only problem in that expression, and it was not the only confusing thing in the question. When these pile up, they make the question too confusing, and it becomes less clear where the problems come from. Now all these problems are corrected and the question is clear. – Szabolcs Jun 14 '14 at 13:26
  • @Szabolcs Well, I think our two comments above now answer the question. Or we are both wrong ;o) – Öskå Jun 14 '14 at 13:29
  • @Öskå Post an answer! – Szabolcs Jun 14 '14 at 13:29
  • You are BOTH right:)) it's perfect! I tried almost the same but not exactly, so I could never get a list. Can anyone explain me why the "&" sign is needed there? that's why I'm not getting yet. Is it for the matrix that you state at the end? And @Öskå: do I always have to take "#" before the brackets if I want to take every row? I tried it like this: MemberQ[M[[#,2]],1]. – Phily Jun 14 '14 at 13:30
  • @Phily If you come from MATLAB and you're e beginner, just keep two things in mind: 1. Never use For, it makes things unreadable, it's slow, it's error-prone. Use Do instead, which is a better equivalent of MATLAB's for. 2. If you need to collect results from a Do, use Table. It has the same syntax as Do, but it collects results into a Table, and it's much more commonly used than Do. This will start you on the road for using functional constructs. Many uses of Table can be replaced with other functional approach, such as Map (a bit like arrayfun). – Szabolcs Jun 14 '14 at 13:33
  • Take a look at this and this for the particular issue with # and &. – Öskå Jun 14 '14 at 13:35
  • @Phily To be fair, For has its uses, but I think that while you're a beginner, it's a good approach simply not to use it. If you get more proficient in Mathematica, you'll see when For is more appropriate anyway. This problem could be written using Table as Table[MemberQ[M[[i,2]], 1], {i, 1, Length[M]}]. It's better as Table[MemberQ[row[[2]], 1], {row, M}]. It's even better as the Map (/@) we wrote above. Just trying to move step by step from a for-loop-like style to Map. – Szabolcs Jun 14 '14 at 13:36
  • @Szabolcs Thanks very much for the tipp, I'll keep that in mind for the future. Already thought the for-function isn't that comfortable to work with in Mathematica:) – Phily Jun 14 '14 at 13:39
  • Also check out MATLink. It'll let you call any existing MATLAB code you might have from Mathematica (of course there's some performance overhead for each call). – Szabolcs Jun 14 '14 at 13:39
  • @Szabolcs Thanks very very much for your kind help!! – Phily Jun 14 '14 at 13:45

1 Answers1

5

All you need is to do Map MemberQ over the second column (a.k.a. [[2]]) of each lines:

M = {{A,B,C},{1,{1,3,6,7},0},{1,{3,7,9},1}}
MemberQ[#[[2]], 1] & /@ M

{False, True, False}

An equivalent would be (thanks to Mr.Wizard):

MemberQ[#2, 1] & @@@ M
Öskå
  • 8,587
  • 4
  • 30
  • 49
  • 1
    Slightly cleaner and faster: MemberQ[#2, 1] & @@@ M (+1) – Mr.Wizard Jun 14 '14 at 14:49
  • @Mr.Wizard I'm surprised that it's faster (haven't tried), but it's definitely more appealing to me. – Szabolcs Jun 14 '14 at 15:31
  • @Szabolcs The statement only holds for unpacked data, but if we're working with packed data numeric methods are likely to be much faster (as I'm sure you know; stating it for others). – Mr.Wizard Jun 14 '14 at 15:55