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!
{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 returnTrue. If you meanMemberQ[M[[2,2]],1], please write it as such. – Szabolcs Jun 14 '14 at 13:21Bis a list then? – Szabolcs Jun 14 '14 at 13:22MemberQ[#[[2]], 1] & /@ Mwhat you are looking for? – Öskå Jun 14 '14 at 13:24MemberQ[#, 1] & /@ M[[All, 2]]. – Szabolcs Jun 14 '14 at 13:24For, it makes things unreadable, it's slow, it's error-prone. UseDoinstead, which is a better equivalent of MATLAB'sfor. 2. If you need to collect results from aDo, useTable. It has the same syntax asDo, but it collects results into aTable, and it's much more commonly used thanDo. This will start you on the road for using functional constructs. Many uses ofTablecan be replaced with other functional approach, such asMap(a bit likearrayfun). – Szabolcs Jun 14 '14 at 13:33#and&. – Öskå Jun 14 '14 at 13:35Forhas 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 whenForis more appropriate anyway. This problem could be written usingTableasTable[MemberQ[M[[i,2]], 1], {i, 1, Length[M]}]. It's better asTable[MemberQ[row[[2]], 1], {row, M}]. It's even better as theMap(/@) we wrote above. Just trying to move step by step from a for-loop-like style toMap. – Szabolcs Jun 14 '14 at 13:36