Message Boards Message Boards

0
|
4313 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How can I create a function that threads through all lists?

Posted 9 years ago

In Mathematica, standard functions like Sign[x] or Log[x] thread, so that Sign[{1,-2},{0,5}] = [{1,-1},{0,1}]. I want a simple function that works like Sign[x], except that it returns 1 with a 0 input and 0 for negative inputs. Here's my function.

Sn[x_] := If[x<0,0,1]

I want

Sn[{1,-2},{0,5}]

[{1,0},{1,1}]

I get

Sn[{1,-2},{0,5}]

If[{1,0},{1,1}<0,0,1]

I tried playing with Thread and Map, but nothing works. Can someone help? Thanks.

POSTED BY: Tommy Volk
2 Replies

You want:

Sn[x_] := If[x < 0, 0, 1];
SetAttributes[Sn, Listable];
Sn[{{1, -2}, {0, 5}}]
Posted 9 years ago

Christopher. Thank you sooo much. It works great.

POSTED BY: Tommy Volk
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract