Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.3K Views
|
3 Replies
|
1 Total Like
View groups...
Share
Share this post:

Max function on colored data?

Posted 10 years ago

Hi all, I have a quick question. Assume I have a data like

  data = {1, 2, 5, 3, 4, 7, 6};

and want to change the color of data

data2 = Table[Style[data[[i]], Red], {i, Length@data}]

Max@data2

Why does Max function not work on colored data? Sort works, for example. Is there any way to color the data and find max of new data? Thanks in advance..

POSTED BY: Okkes Dulgerci
3 Replies
Posted 10 years ago

I see now, thanks..

POSTED BY: Okkes Dulgerci

As Jim stated, they are not a list of numbers,

You can separate the parts of the list into two columns, the first containing the actual number (the one you want to sort) and the other containing the color:

In[1]:= data2[[All, 1]]
Out[1]:= {1, 2, 5, 3, 4, 7, 6}

In[2]:= data2[[All, 2]]
Out[2]:= {RGBColor[1, 0, 0], RGBColor[1, 0, 0], RGBColor[1, 0, 0], RGBColor[
 1, 0, 0], RGBColor[1, 0, 0], RGBColor[1, 0, 0], RGBColor[1, 0, 0]}

colorrow

The function MaximalBy allows you to specify a function the result of which will be the basis for your maximum, you can write:

MaximalBy[data2, First]

Which is equivalent to:

MaximalBy[data2, #[[1]]&]

i.e. it finds the maximum by looking at the first entry of each row (this is where you keep your values). Similarly, you can use SortBy to sort your colored lists.

Regards, Patrik

POSTED BY: Patrik Ekenberg
Posted 10 years ago

To see why use FullForm on both data and data2:

data // FullForm
(* List[1, 2, 5, 3, 4, 7, 6] *)

data2 // FullForm
(* List[Style[1, RGBColor[1, 0, 0]], Style[2, RGBColor[1, 0, 0]],  
  Style[5, RGBColor[1, 0, 0]], Style[3, RGBColor[1, 0, 0]], 
  Style[4, RGBColor[1, 0, 0]], Style[7, RGBColor[1, 0, 0]], 
  Style[6, RGBColor[1, 0, 0]]] *)

data2 is not a list of numbers.

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