Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.1K Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Generating a dummy variable

Posted 11 years ago
POSTED BY: Florian Bannwart
6 Replies

It worked perfectly, thank you very much Gianluca!

POSTED BY: Florian Bannwart

You should wrap your data into a list

{{6., 7.}, {6., 4.}, {1., 6.}, {4., 6.}, {7., 5.}, {6., 2.}, {6., 7.}, {6., 4.}}

and then apply the replacement rule outside the final bracket:

  {{6., 7.}, {6., 4.}, {1., 6.}, {4., 6.}, {7., 5.}, {6., 2.}, {6., 7.}, {6., 4.}}/. {a_, b_} :> {a, b, If[a >= b, 1, 0]}

The repacement rule instructs Mathematica to replace any list of two elements of the form {a,b} with the triplet {a,b,variable}, where the variable is 1 or 0 according to the values of a,b. The underscores in {a_, b_} mean that a and b are not meant to be literally the two symbols a and b, but they are patterns to which we assign the tags a and b for the purposes of the replacement.

The rule {a_, b_} :> {a, b, If[a >= b, 1, 0]} fails if your matrix is 2 by 2, in which case you should use the more elaborate version {a_?NumericQ, b_} :> {a, b, If[a >= b, 1, 0]} ,which checks if a is a number.

There are other ways of solving your problem, that may be more obscure but a lot faster for very large matrices.

POSTED BY: Gianluca Gorni

It's a nx2 matrix, containing integers.

i.e.:

{6., 7.}, {6., 4.}, {1., 6.}, {4., 6.}, {7., 5.}, {6., 2.}, {6., 7.}, {6., 4.}

POSTED BY: Florian Bannwart

What is the format of your matrix? Is it a list of lists? Can you give a small sample?

POSTED BY: Gianluca Gorni
POSTED BY: Florian Bannwart

Something like this?

{{1, 2}, {2, 1}} /. {a_?NumericQ, b_} :> {a, b, If[a >= b, 1, 0]}
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard