5
$\begingroup$

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.

$\endgroup$
11
$\begingroup$

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
|improve this answer|||||
$\endgroup$
7
$\begingroup$

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

|improve this answer|||||
$\endgroup$
  • $\begingroup$ it was for position because I could not find other tricks. Your solution opens new door for me. THank You ! $\endgroup$ – 500 Feb 9 '12 at 16:09
7
$\begingroup$

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

|improve this answer|||||
$\endgroup$
2
$\begingroup$

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.

|improve this answer|||||
$\endgroup$

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.