In an Excel file, I have some numbers which I will use as the indices of arrays. I want to read those numbers and assign them as the array indices in Mathematica.

For instance, in Excel I have

enter image description here

and I want the rows to be imported as follows in Mathematica:

{{x[[1]], x[[1]], x[[10]], x[[12]], x[[1]], x[[2]], x[[11]], x[[3]]},
 {x[[1]], x[[1]], x[[12]], x[[14]], x[[1]], x[[3]], x[[13]], x[[4]]},
 ... }

Link to download the excel file: http://www.datafilehost.com/d/477a157f

  • $\begingroup$ Could you please explain more? I am a beginner user. $\endgroup$ – Enthusiastic Engineer Jun 17 '14 at 8:28
  • $\begingroup$ The array numbers to be imported are as the picture above. My data is that. $\endgroup$ – Enthusiastic Engineer Jun 17 '14 at 8:31
  • $\begingroup$ No, that is the excel file in which the numbers are stored, I want mathematica to use those numbers as array counters. But I don't know how. $\endgroup$ – Enthusiastic Engineer Jun 17 '14 at 8:33
  • $\begingroup$ I added the data to the question. $\endgroup$ – Enthusiastic Engineer Jun 17 '14 at 8:38
  • $\begingroup$ Question updated $\endgroup$ – Enthusiastic Engineer Jun 17 '14 at 8:50

Here's another version:

Import["~/Downloads/NodeNumbers.xlsx"] /. ind_Real :> x[[Floor@ind]]
| improve this answer | |
  • $\begingroup$ It worked for me. I used the following syntaxes in the next comments: $\endgroup$ – Enthusiastic Engineer Jun 17 '14 at 10:29
  • $\begingroup$ x = Flatten[Import[FileNameJoin[{NotebookDirectory[], "x.xlsx"}]]]; $\endgroup$ – Enthusiastic Engineer Jun 17 '14 at 10:29
  • $\begingroup$ Flatten[Import[ FileNameJoin[{NotebookDirectory[], "NodeNumbers.xlsx"}]] /. ind_Real :> x[[Floor@ind]], 1] $\endgroup$ – Enthusiastic Engineer Jun 17 '14 at 10:33

Given your data in the link, e.g.:

arrayList = 
 Map[x[[#]] &, 
  First@Round[Import["c:\\users\\rasher\\downloads\\nodenumbers.xlsx"]], {2}];

will give you the symbol arrayList containing the list of lists of x indexed using the values in the spreadsheet.

Note that if x does not already exist with enough values, you'll get error messages. You can append //Quiet to the above if you just want a list of lists of indexed x - if/when you create and fill x, arrayList entries will take on the appropriate values. The messages (if x does not exist in proper form) are from attempting to reference something non-existent. You could also Hold the entries to avoid this, but I'm not clear what the end result you're after is...

If there's something else you had in mind, you'll need to further clarify your question.

| improve this answer | |
  • $\begingroup$ note you can do the Map on level 1 just as well : x[[#]] & /@ First@Round[Import["file.xlsx"]] -- might be a tad faster, then again the excel import performance likely dwarfs that difference. $\endgroup$ – george2079 Jun 17 '14 at 17:16

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.