2
$\begingroup$

This question already has an answer here:

I would like to assign a list of images of patients to the patient name. To do so I created a list with all the patients names and now I want to assign to every element in the list the correct images. The problem is that Mathematica assign the images to the position in the list not the the element contained in it.

Example:

list = {patient1, patient2, patient3, ...}; 
list[[1]] = Import[images]

I wish Mathematica allowed patient1 = Import[images], but it doesn't.

How can I solve this problem?

$\endgroup$

marked as duplicate by Mr.Wizard list-manipulation Dec 20 '16 at 18:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ First guess is that Pattern Matching should help. $\endgroup$ – Valacar Dec 19 '16 at 16:05
  • $\begingroup$ Proposed duplicate: mathematica.stackexchange.com/q/6511/121 (see the many Linked questions in the right sidebar of that one) $\endgroup$ – Mr.Wizard Dec 19 '16 at 17:05
3
$\begingroup$
patientsNames = {"mickey1", "mickey2", "mickey3"};

patientsImages = {
  Import["http://i.imgur.com/Xppxuqb.gif"], 
  Import["http://i.imgur.com/XBY9QB6.gif"], 
  Import["http://i.imgur.com/d2P3b1Q.gif"]};

patients = MapThread[{#1, #2} &, {patientsNames, patientsImages}]

enter image description here

patients[[1]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Maybe Transpose@{patientsNames, patientsImages} instead of MapThread? $\endgroup$ – BlacKow Dec 19 '16 at 19:56
1
$\begingroup$

You may use Association.

I'll use ExampleData instead of Import.

images = Association @@ MapThread[Rule,
    {
     {"patient1", "patient2", "patient3"},
     {
      ExampleData /@ {{"TestImage", "Aerial"}, {"TestImage", "Aerial2"}},
      ExampleData /@ {{"TestImage", "Flower"}, {"TestImage", "Girl"}},
      ExampleData /@ {{"TestImage", "Sailboat"}, {"TestImage", "Splash"}}
      }
     }
    ];

The above creates an Association where the images can be accessed by the patient's name.

images["patient1"]

Mathematica graphics

As you appear to be working with structured data I suggest you read the Associations guide and the Computation with Structured Datasets guide. There are many functions and features you may benefit from.

Hope this helps.

$\endgroup$
1
$\begingroup$

Indexed variables can be useful in situations like the one you describe.

Using mrz's nice little set of images

patientsImages

images

we can make the assignment with Do

 Do[patient[i] = patientsImages[[i]], {i, Length[patientsImages]}]

then

Definition[patient]

defs

$\endgroup$

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