1
$\begingroup$

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"}}}
$\endgroup$
  • 1
    $\begingroup$ How about coords/@data? Without data it is difficult to help you $\endgroup$ – Yves Klett Feb 27 '14 at 22:04
2
$\begingroup$

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}}
$\endgroup$
  • $\begingroup$ it works like clockwork :) thank you! Some of these commands are new to me but, i'll try to get my head around it. $\endgroup$ – user 3 50 Feb 27 '14 at 22:34
  • $\begingroup$ Map is very common -- it's one way to replace a Do or For looping structure. Flatten is really useful for removing extra parentheses. $\endgroup$ – bill s Feb 27 '14 at 22:35
  • $\begingroup$ your explanations are very good, i think i already understand it better now $\endgroup$ – user 3 50 Feb 27 '14 at 22:38
  • 1
    $\begingroup$ @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. $\endgroup$ – Michael E2 Feb 28 '14 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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