TransWikia.com

Converting a list to an array

Mathematica Asked on December 15, 2020

Let’s assume that I have a list that I can access like list[[i, j]]

How can I convert it to an array such that:

my_array[i, j] = list[[i, j]]

In the simplest way (avoiding loops). Also, ideally shifting array index such that my_array[i, j] = list[[i+1, j+1]] (to have the [0, 0] of the array matching the first element from the "matrix" [[1, 1]])

2 Answers

What you're calling my_array may be thought of as a function, which you can assign like:

myfun[i_, j_] := list[[i, j]];

or, to shift the indices:

myfun[i_, j_] := list[[i+1, j+1]];

Answered by bill s on December 15, 2020

SeedRandom[1]
list = RandomInteger[10, {4, 5}]
 {{1, 4, 0, 7, 0}, {0, 8, 6, 0, 4}, {1, 8, 5, 1, 1}, {1, 3, 2, 10, 1}}
ClearAll[myArray]
myArray = Indexed[list, 1 + {##}] /. Indexed[_, ___] :> Undefined &;

{myArray[0, 1], myArray[3, 2], myArray[4, 6]}
 {4, 2, Undefined}

Answered by kglr on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP