Search code examples
arrayslistindicesmaxima

Choosing multiple values from a Maxima list based on a list of indices


Let's say that I had a Maxima list of indices, say indexlist = [1,3,5], and a list of values, for example valuelist = [1,2,3,4,5]. What is the syntax for choosing the elements of valuelist based on the indices in indexlist on a single line? I've tried

values : valuelist[indexlist]; /*Should return [1,3,5]*/

which obviously didn't work since I'm feeding a list to the selection operator [], that expects a nonnegative integer.


Solution

  • There isn't a built-in syntax for that. The idiom which I usually use is:

    somevals : makelist (valuelist[i], i, indexlist);
    

    Note that values is a built-in global variable; it is the list of symbols which have values assigned to them. See ? values and maybe also ? infolists.