Group Abstract Group Abstract

Message Boards Message Boards

1
|
9.3K Views
|
4 Replies
|
1 Total Like
View groups...
Share
Share this post:

Generalize this function for ratio of a list element?

Posted 7 years ago
4 Replies

this is also a solution

rationormalize4[list_?VectorQ, pos_Integer: 4] := list/list[[pos]]
rationormalize4[list_?ListQ, pos_Integer: 4] := rationormalize4[#, pos] & /@ list

Maybe not as compact but will not give errors for non list inputs

In[4]:= rationormalize4[{{1, 2, 4, 6, 7}, "a", 1}, 2]

Out[4]= {{1/2, 1, 2, 3, 7/2}, rationormalize4["a", 2], rationormalize4[1, 2]}

In[5]:= rationormalize4[{{1, 2, 4, 6, 7}, "a", 1}, 2.2]

Out[5]= rationormalize4[{{1, 2, 4, 6, 7}, "a", 1}, 2.2]

An it can handle uneven dephts

In[6]:= rationormalize4[{{1, 3, 1, 6}, {{1, 3, 1, 6}, {2, 5, 7, 9}}}, 2]

Out[6]= {{1/3, 1, 1/3, 2}, {{1/3, 1, 1/3, 2}, {2/5, 1, 7/5, 9/5}}}

In[8]:= ratioNormalize5[{{1, 3, 1, 6}, {{1, 3, 1, 6}, {2, 5, 7, 9}}}, 2]

During evaluation of In[8]:= Thread::tdlen: Objects of unequal length in {{1/3,1,1/3,2},{2/5,1,7/5,9/5}} {5/2,1,5/7,5/9} cannot be combined.

Out[8]= {{1/3, 1, 1/3, 2}, {{1/3, 1, 1/3, 2}, {2/5, 1, 7/5, 9/5}} {5/2, 1, 5/7, 5/9}}
POSTED BY: Martijn Froeling

Update: I think this version of the function from dan Erickson is working:

ratioNormalize5[list_, pos_ : 4] := Map[(#/#[[pos]]) &, list, -2]

Try this formulation:

ratioNormalize[list, pos_ 4] := Map[(#/#[[pos]]) &, list, -2]
POSTED BY: dan Erickson

you could just map all the time? it is not very elegant but a typical mma programming thing to do hehe. and this also saves you the efforts to add further DownValues which could lead to new problems. "The less DownValues the better." That's the golden rule of programming.

data = {{1, 3, 1, 6}, {2, 5, 7, 9}};
rationormalize4[#, 2] & /@ data

and if you're like me, then the second golden rule of programming is to avoid using code which is confusing or hard to understand now. If you have difficulties understanding the code now, you will have the same difficulties then in future again, when you revisit the code (for revision, maintenance, sharing, whatever) because we humans tend to forget complicated things we once understood and only remember well the few things which were clear, short, and simple.

if you have a choice, then the simplest solution is the preferable solution. rock solid, robust, straight-forward, and you have full confidence in employing it.

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