How do I generalize this function so it can take any matrix and, for each row, take the ratio of each element to the n’th element of that row? Mapping works if I want to use the default position I set, but not if I want to specify a position.
Function:
rationormalize4[list_, pos_ : 4] := list/list[[pos]]
Here’s what works and what doesn’t…
Apply to a list with the default position of 4 - works:
In[76]:= rationormalize4[{1, 4, 2, 6}]
Out[76]= {1/6, 2/3, 1/3, 1}
Map over a matrix a.k.a. list of lists with the default position of 4 - works:
In[78]:= Map[rationormalize4, {{1, 3, 1, 6}, {2, 5, 7, 9}}]
Out[78]= {{1/6, 1/2, 1/6, 1}, {2/9, 5/9, 7/9, 1}}
Apply to a list of lists with a non-default position - doesn’t work, thinks pos_ is the second list instead of the second element. Is there a way to write in a level specification?:
In[80]:= rationormalize4[{{1, 3, 1, 6}, {2, 5, 7, 9}}, 2]
During evaluation of In[80]:= Thread::tdlen: Objects of unequal length in {{1,3,1,6},{2,5,7,9}} {1/2,1/5,1/7,1/9} cannot be combined.
Out[80]= {{1, 3, 1, 6}, {2, 5, 7, 9}} {1/2, 1/5, 1/7, 1/9}
Setting attributes to Listable - doesn’t work because then the function thinks it has to take the second element of each element and gets confused:
In[81]:= SetAttributes[rationormalize4, Listable]
In[82]:= rationormalize4[{{1, 3, 1, 6}, {2, 5, 7, 9}}, 2]
During evaluation of In[82]:= Part::partd: Part specification 1[[2]] is longer than depth of object.
During evaluation of In[82]:= Part::partd: Part specification 3[[2]] is longer than depth of object.
During evaluation of In[82]:= Part::partd: Part specification 1[[2]] is longer than depth of object.
During evaluation of In[82]:= General::stop: Further output of Part::partd will be suppressed during this calculation.
Out[82]= {{1/1[[2]], 3/3[[2]], 1/1[[2]], 6/6[[2]]}, {2/2[[2]], 5/
5[[2]], 7/7[[2]], 9/9[[2]]}}
Any help is appreciated - I’m making a bunch of data analysis functions for chemistry.