Table[{f1, f2, f3}, (x, xmin, xmax}]
and anytime the value of f2 goes negative I want that value printed in
Red.
What's a simple, compact way to code this?
Before displaying the result on screen, replace each negative value:
{1, 2, -1, -2} /. x_?Negative -> Style[x, Red]
--
David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/
"AES" <sie...@stanford.edu> wrote in message
news:gci1tn$ji$1...@smc.vnet.net...
Print[Table[{Sin[x], If[# < 0, Style[#, Red], #] &@Cos[x], x}, {x, 1,
10}]]
{{Sin[1],Cos[1],1},{Sin[2],Cos[2],2},{Sin[3],Cos[3],3},{Sin[4],Cos[4],\
4},{Sin[5],Cos[5],5},{Sin[6],Cos[6],6},{Sin[7],Cos[7],7},{Sin[8],Cos[\
8],8},{Sin[9],Cos[9],9},{Sin[10],Cos[10],10}}
Bob Hanlon
---- AES <sie...@stanford.edu> wrote:
=============
I'm Printing (to the screen) a Table like, let's say,
Table[{f1, f2, f3}, (x, xmin, xmax}]
and anytime the value of f2 goes negative I want that value printed in
Red.
What's a simple, compact way to code this?
--
Bob Hanlon
>From other posts I believe you don't actually appreciate "functional"
programming constructs to much. Anyway, this problem is a good
demonstration how much simpler and clearer this can be than writing
loops. Note that I tried to avoid arcane shortcuts like # and & for
the formulation of the pure function:
Map[
Function[{x}, If[x < 0, Style[x, Red], x]],
Table[RandomReal[{-1, 1}, {3}], {x, 1, 3}],
{2}
]
Of course for presentation purposes you might want to wrap the result
with a Grid or MatrixForm.
hth,
albert
correcting my previous post: pattern matching is probably even better in
this case:
ReplaceAll[
Table[RandomReal[{-1, 1}, {3}], {x, 1, 3}],
x_?Negative :> Style[x, Red]
]
One possible way:
f1 := RandomInteger[{-1, 1}];
f2 := (-1)^x;
f3 := Sin[N[x]];
redneg[expr_] := If[expr < 0, Style[expr, Red], expr, expr]
With[{xmin = 0, xmax = 10},
Table[{f1, redneg[f2], f3}, {x, xmin, xmax}]]
Regards,
-- Jean-Marc
Cheers -- Sjoerd
>I'm Printing (to the screen) a Table like, let's say,
>Table[{f1, f2, f3}, (x, xmin, xmax}]
>and anytime the value of f2 goes negative I want that value printed
>in Red.
>What's a simple, compact way to code this?
Define a function to color as you like. For example,
f[x_] := Style[x, Red] /; x < 0
f[x_] := x /; x >= 0
Now
Table[{f1, f@f2, f3}, (x, xmin, xmax}]
will have all negative values of f2 red and all other entries the usual col=
or.
Table[{f1, f2, f3}, (x, xmin, xmax}]
and anytime the value of f2 goes negative I want that value printed in
Red.
What's a simple, compact way to code this?
Try this (here random numbers instead of your terms are to be precise)
TableForm[
Table[{f1, Style[ f2, If[f2 >= 0, Black, Red]],
f3} /. {f1 -> RandomReal[],
f2 -> (-1)^RandomInteger[]*RandomReal[], f3 -> RandomReal[]}, {i,
0, 5}]]
--
Alexei Boulbitch, Dr., Habil.
Senior Scientist
IEE S.A.
ZAE Weiergewan
11, rue Edmond Reuter
L-5326 Contern
Luxembourg
Phone: +352 2454 2566
Fax: +352 2454 3566
Website: www.iee.lu
This e-mail may contain trade secrets or privileged, undisclosed or otherwise confidential information. If you are not the intended recipient and have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal from your system. Thank you for your co-operation.
If[# < 0, Style[#, Red], #] & /@ Table[RandomReal[{-1, 1}], {10}]
Regards
Jens
> I'm Printing (to the screen) a Table like, let's say,
>
> Table[{f1, f2, f3}, (x, xmin, xmax}]
>
> and anytime the value of f2 goes negative I want that value printed in
> Red. What's a simple, compact way to code this?
Of the dozen or so replies, I'd say that
> Table[{f1, Style[f2, If[f2 < 0, Red, Black]], f3}, {x, -1, 5}]
>
>
> Bob Hanlon
is by far the best: simple, clean, you can see what it does, no arcane
symbols, no cross effects on anything else outside that entry (and, the
solution I'd already come up with on my own).
If the conditional in the If[ ] statement got lengthy, to avoid
cluttering up the Table with it I might go to the alternative suggestion
> redneg[expr_] := If[cond[expr], Style[expr, Red], expr, expr]
>
> With[{xmin = 0, xmax = 10},
< Table[{f1, redneg[f2], f3}, {x, xmin, xmax}]]
<
> Regards, Jean-Marc
Or, just define the conditional separately, outside the Table,
cond[expr_] : = <<something>>
and use
Style[f2, If[fcond[f2], Red, Black]]
in the Table.
Would Plot[redneg[f[x]], {x, xmin, xmax}] make the plot turn black
below the x axis??? Have to try it and see . . .
Plot[(x - 1) (x - 4), {x, 0, 5},
ColorFunction -> Function[{x, y}, If[y < 0, Blue, Red]],
ColorFunctionScaling -> False]
Bob Hanlon
---- AES <sie...@stanford.edu> wrote:
=============
Original post:
cond[expr_] : = <<something>>
and use
in the Table.
--
Bob Hanlon
>Original post:
>>redneg[expr_] := If[cond[expr], Style[expr, Red], expr, expr]
>Would Plot[redneg[f[x]], {x, xmin, xmax}] make the plot turn black
>below the x axis??? Have to try it and see . . .
No, this will simply generate an error message telling you the
Plot has no idea what to do with functions that do not return
numeric values. Plotting color is set by specifying a PlotStyle.
To get different plot colors for positive/negative values you
would need to define a custom ColorFunction to set the plot color.
you could use Style in many different ways, e.g.
Table[If[i < 0, Style[i, Red], Style[i, Green]], {i, -5, 5}]
Regards,
Yves
AES schrieb: