2
$\begingroup$

This question already has an answer here:

I'm new to Mathematica. I hope this is not a too stupid question, but I couldn't find a solution in the Mathematica documentation.

My question is about creating a function to iterate over the elements in a table. For example, suppose I have a table

a = {{1, 2, 34, 2}, {1, 2, 8, 3}, {12, 4, 6, 2}}

I want to build another table such that when an element of a is < 10,the corresponding the new table is 1 , and when element of a is >= 10, the corresponding the new table is 0.

I really don't know how to do it.

$\endgroup$

marked as duplicate by Sascha, MarcoB, LCarvalho, Community Aug 14 '17 at 15:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$
b = a /. { x_ /; x < 10 -> 1, y_ /; y >= 10 -> 0}
$\endgroup$
  • $\begingroup$ Thank you very much! This was easier than i thought! $\endgroup$ – Mudy Fa Aug 11 '17 at 13:47
3
$\begingroup$

Do you mean this?

Map[If[# < 10, 1, 0] &, a, {2}]

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

Or

Map[Boole[# < 10]&, a, {2}]

Or

Clip[a, {10, 10}, {1, 0}]
$\endgroup$
  • $\begingroup$ Vielen Dank! Ihrem Profil zu Urteile sind Sie aus Berlin. Mein Englisch ist schlecht, wie Sie sehen können! Danke nochmals! :) $\endgroup$ – Mudy Fa Aug 11 '17 at 13:52
  • $\begingroup$ Gern geschehen :) $\endgroup$ – eldo Aug 11 '17 at 15:45

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