Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have quite a lot of data in an Excel file, which I have to use in further calculations. The transformation should map a data point with thre coordinates into a two coordinate result. Example: $(r,g,b)\mapsto(r/2 + g, r \, tan(\pi/3)/2)$.

So far I have tried the following, but it doesn't work since the result is the same as the imported data.

coords[{r_, g_, b_}] := {r/2 + g, r Tan[Pi/3]/2};
data = Import["file.xlsx"]
coords[data]

Edit

OK, i'm sorry for not posting the data earlier:

{{{"0.1", "0.65", "0.25"}, {"0.8", "0.1", "0.1"}, {"0.7", "0.15", "0.15"}, 
  {"0.6", "0.1", "0.3"}, {"0.5", "0.2", "0.3"}, {"0.4", "0.1", "0.5"}, 
  {"0.3", "0.3", "0.4"}, {"0.2", "0.1", "0.7"}, {"0.1", "0.8", "0.1"}}}
share|improve this question
1  
How about coords/@data? Without data it is difficult to help you –  Yves Klett Feb 27 at 22:04

1 Answer 1

up vote 2 down vote accepted

You have two issues... first, the data is in the form of strings, so needs to be changed into numbers (this is the ToExpression). The Flatten removes an extra pair of brackets, and then you can Map the coords function onto the data. The symbols /@ are shorthand for this mapping.

stringData = {{{"0.1", "0.65", "0.25"}, {"0.8", "0.1", "0.1"}, 
 {"0.7", "0.15", "0.15"}, {"0.6", "0.1", "0.3"}, {"0.5", "0.2", "0.3"}, 
 {"0.4", "0.1", "0.5"}, {"0.3", "0.3", "0.4"}, {"0.2", "0.1", "0.7"}, {"0.1", "0.8", "0.1"}}}; 
data = Flatten[ToExpression[stringData], 1];
coords[{r_, g_, b_}] := {r/2 + g, r Tan[Pi/3]/2};
coords /@ data

Your output is then:

{{0.7, 0.0866025}, {0.5, 0.69282}, {0.5, 0.606218}, {0.4, 0.519615}, 
 {0.45, 0.433013}, {0.3, 0.34641}, {0.45, 0.259808}, {0.2, 0.173205}, {0.85, 0.0866025}}
share|improve this answer
    
it works like clockwork :) thank you! Some of these commands are new to me but, i'll try to get my head around it. –  user 3 50 Feb 27 at 22:34
    
Map is very common -- it's one way to replace a Do or For looping structure. Flatten is really useful for removing extra parentheses. –  bill s Feb 27 at 22:35
    
your explanations are very good, i think i already understand it better now –  user 3 50 Feb 27 at 22:38
1  
@user12073 You can also use Dot: data.Transpose@{{1/2., 1., 0.}, {Tan[Pi/3]/2., 0., 0.}}, which can be much faster on large data sets. –  Michael E2 Feb 28 at 4:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.