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.

Please consider :

 subIDs = {"AK6", "CF11", "CL4", "FC21", "MK5"};

 subColors = {LightOrange, LightBlue, LightYellow, LightGreen, LightRed}

 Graphics[{subColors[[Flatten@Position[subIDs, #, 3]]],Rectangle[]}]&/@subIDs

enter image description here

How could i avoid the output of position to be 1 for example instead of {1} which seems to be my problem.

share|improve this question
add comment

4 Answers 4

up vote 10 down vote accepted

You can add a parameter to Position so it returns only one result and apply First or Partas needed

subIDs = {"AK6", "CF11", "CL4", "FC21", "MK5"};

subColors = {LightOrange, LightBlue, LightYellow, LightGreen, LightRed}

Graphics[{subColors[[Position[subIDs, #, 3, 1][[1, 1]]]], 
    Rectangle[]}] & /@ subIDs

Furthermore, you could also use Extract that's more suited to Position's output

subIDs = {"AK6", "CF11", "CL4", "FC21", "MK5"};

subColors = {LightOrange, LightBlue, LightYellow, LightGreen, LightRed}

Graphics[{First@Extract[subColors, Position[subIDs, #, 3, 1]], 
    Rectangle[]}] & /@ subIDs
share|improve this answer
add comment

Does it have to be Position? To solve your example problem, a pattern-based solution may be easier:

subIDs = {"AK6", "CF11", "CL4", "FC21", "MK5"};
subColors = {LightOrange, LightBlue, LightYellow, LightGreen, LightRed};
pattern = Thread[subIDs -> subColors]
{"AK6" -> RGBColor[1, 0.9, 0.8], "CF11" -> RGBColor[0.87, 0.94, 1], "CL4" -> RGBColor[1, 1, 0.85], "FC21" -> RGBColor[0.88, 1, 0.88], "MK5" -> RGBColor[1, 0.85, 0.85]}
Graphics[{# /. pattern, Rectangle[]}] & /@ subIDs

enter image description here

share|improve this answer
    
it was for position because I could not find other tricks. Your solution opens new door for me. THank You ! –  500 Feb 9 '12 at 16:09
add comment

Why not

  Graphics[{#, Rectangle[]}] & /@ subColors

Or, perhaps, more generally,

  GraphicsRow@MapThread[Graphics[{#1, Rectangle[], Black, 
  Inset[Text@#2, {.5, .5}]}] &,  {subColors, subIDs}]

to get

enter image description here

share|improve this answer
add comment

Others have shown you better ways to solve your example problem. Just in case your example was just made up to learn how to do what your title says I think Sequence is what you are after:

Graphics[{
  subColors[[Sequence @@ Flatten@Position[subIDs, #, 3]]],Rectangle[]
}] & /@ subIDs

@@ is the short form of Apply which lets you exchange the head of an expression.

share|improve this answer
add comment

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.