Group Abstract Group Abstract

Message Boards Message Boards

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

Compare the values of variables and print only the names of variable?

Posted 10 years ago

Hello to everybody. I try to understand Wolfram Mathematica. I have list of variables and their values. a=5, b=6, c=6, d=2 I have to compare them to find maximum and print only the names of variables with text. There is two maximum options (b and c). For example: "You have to choose b,c" I know how to compare but I find only the value of variables.

m = {a = 5, b = 6, c = 6, d = 2}; Max[m]

I see the code is not right for the task. How to compare the values of variables and print only the names of variables with the text? Can you explain me your code with comments? I want to understand.

POSTED BY: Petr Petr
2 Replies
Posted 10 years ago

Hello Michael, Thank you very much.
I have two questions. 1) I calculate a, b, c, d. For example a=10+8-13=5 and etc. Is it possible transform a = 5, b = 6, c = 6, d = 2 into m = {{a, 5}, {b, 6}, {c, 6}, {d, 2}}? I find this way:

In[101]=m = MapThread[
  list, {{a, b, c, d}, {10 + 8 - 13, 5 + 5 - 4, 7 + 5 - 6, 
    3 + 4 - 5}}]; t1 = Sort[m, #1[[2]] > #2[[2]] &];
First[SplitBy[t1, #[[2]] &]]
out[102]={list[c, 6], list[b, 6]}

Is there another way?

2) What does "#[[2]] &" mean?

POSTED BY: Petr Petr
Posted 10 years ago

Hello Petr,

you should not assign values to the variables. Instead keep the variable name and the associated value in a (sub-)list:

In[1]:= m = {{a, 5}, {b, 6}, {c, 6}, {d, 2}}

Out[1]= {{a, 5}, {b, 6}, {c, 6}, {d, 2}}

Next you sort the sublists according to the 2nd list member which holds the value:

In[2]:= t1 = Sort[m, #1[[2]] > #2[[2]] &]

Out[2]= {{c, 6}, {b, 6}, {a, 5}, {d, 2}}

Finally you split the list t1 into sublists which identical values and since they are already ordered according to the size of the value, you take the first member:

In[3]:= First[SplitBy[t1, #[[2]] &]]

Out[3]= {{c, 6}, {b, 6}}

If your dataset is very large it may be more efficient to search for the max value without sorting the values.

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