{
  "id": 100467,
  "title": "quick doubt about numpy arrays [SOLVED]",
  "url": "/competitions/champs-scalar-coupling/discussion/100467",
  "author_name": "",
  "post_date": "2019-07-18T18:09:32.114534400Z",
  "votes": 4,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Hi, can someone help me with this numpy question?</p>\n\n<p>I have a (10,10) atom distance matrix like this:</p>\n\n<p>a = array([[ 0,  2,  6,  6, 12,  1,  1,  1,  4,  6],\n       [ 2,  0,  2,  2,  6,  4,  4,  4,  1,  3],\n       [ 6,  2,  0,  5, 11,  7, 11,  7,  4,  0],\n       [ 6,  2,  5,  0,  1, 11,  7,  7,  4,  6],\n       [12,  6, 11,  1,  0, 20, 12, 12,  9, 11],\n       [ 1,  4,  7, 11, 20,  0,  3,  3,  6,  8],\n       [ 1,  4, 11,  7, 12,  3,  0,  3,  6, 13],\n       [ 1,  4,  7,  7, 12,  3,  3,  0,  9,  6],\n       [ 4,  1,  4,  4,  9,  6,  6,  9,  0,  7],\n       [ 6,  3,  0,  6, 11,  8, 13,  6,  7,  0]])</p>\n\n<p>I want to access 5 indices of each array. The indices are:</p>\n\n<p>ind = array([[1, 2, 3, 4, 5],\n       [1, 2, 3, 4, 5],\n       [9, 1, 8, 3, 5],\n       [4, 1, 8, 2, 5],\n       [3, 1, 8, 2, 5],\n       [0, 7, 6, 1, 4],\n       [0, 7, 5, 1, 3],\n       [0, 6, 5, 1, 3],\n       [1, 2, 3, 0, 5],\n       [2, 1, 7, 3, 5]])</p>\n\n<p>For a[0] I want a[0][ind[0]]\nFor a[1] I want a[1][ind[1]]</p>\n\n<p>But I wanted to do it in parallel, without needing a for loop or something like that, to speedup calculations. </p>\n\n<p>Any ideas? I tried googling it but couldn't come up with a solution :/ thanks in advance</p>",
  "messages": [
    {
      "id": "579310",
      "postDate": "07/18/2019 18:09:32",
      "content": "<p>Hi, can someone help me with this numpy question?</p>\n\n<p>I have a (10,10) atom distance matrix like this:</p>\n\n<p>a = array([[ 0,  2,  6,  6, 12,  1,  1,  1,  4,  6],\n       [ 2,  0,  2,  2,  6,  4,  4,  4,  1,  3],\n       [ 6,  2,  0,  5, 11,  7, 11,  7,  4,  0],\n       [ 6,  2,  5,  0,  1, 11,  7,  7,  4,  6],\n       [12,  6, 11,  1,  0, 20, 12, 12,  9, 11],\n       [ 1,  4,  7, 11, 20,  0,  3,  3,  6,  8],\n       [ 1,  4, 11,  7, 12,  3,  0,  3,  6, 13],\n       [ 1,  4,  7,  7, 12,  3,  3,  0,  9,  6],\n       [ 4,  1,  4,  4,  9,  6,  6,  9,  0,  7],\n       [ 6,  3,  0,  6, 11,  8, 13,  6,  7,  0]])</p>\n\n<p>I want to access 5 indices of each array. The indices are:</p>\n\n<p>ind = array([[1, 2, 3, 4, 5],\n       [1, 2, 3, 4, 5],\n       [9, 1, 8, 3, 5],\n       [4, 1, 8, 2, 5],\n       [3, 1, 8, 2, 5],\n       [0, 7, 6, 1, 4],\n       [0, 7, 5, 1, 3],\n       [0, 6, 5, 1, 3],\n       [1, 2, 3, 0, 5],\n       [2, 1, 7, 3, 5]])</p>\n\n<p>For a[0] I want a[0][ind[0]]\nFor a[1] I want a[1][ind[1]]</p>\n\n<p>But I wanted to do it in parallel, without needing a for loop or something like that, to speedup calculations. </p>\n\n<p>Any ideas? I tried googling it but couldn't come up with a solution :/ thanks in advance</p>",
      "rawMarkdown": "Hi, can someone help me with this numpy question?\n\nI have a (10,10) atom distance matrix like this:\n\na = array([[ 0,  2,  6,  6, 12,  1,  1,  1,  4,  6],\n       [ 2,  0,  2,  2,  6,  4,  4,  4,  1,  3],\n       [ 6,  2,  0,  5, 11,  7, 11,  7,  4,  0],\n       [ 6,  2,  5,  0,  1, 11,  7,  7,  4,  6],\n       [12,  6, 11,  1,  0, 20, 12, 12,  9, 11],\n       [ 1,  4,  7, 11, 20,  0,  3,  3,  6,  8],\n       [ 1,  4, 11,  7, 12,  3,  0,  3,  6, 13],\n       [ 1,  4,  7,  7, 12,  3,  3,  0,  9,  6],\n       [ 4,  1,  4,  4,  9,  6,  6,  9,  0,  7],\n       [ 6,  3,  0,  6, 11,  8, 13,  6,  7,  0]])\n\nI want to access 5 indices of each array. The indices are:\n\nind = array([[1, 2, 3, 4, 5],\n       [1, 2, 3, 4, 5],\n       [9, 1, 8, 3, 5],\n       [4, 1, 8, 2, 5],\n       [3, 1, 8, 2, 5],\n       [0, 7, 6, 1, 4],\n       [0, 7, 5, 1, 3],\n       [0, 6, 5, 1, 3],\n       [1, 2, 3, 0, 5],\n       [2, 1, 7, 3, 5]])\n\nFor a[0] I want a[0][ind[0]]\nFor a[1] I want a[1][ind[1]]\n\nBut I wanted to do it in parallel, without needing a for loop or something like that, to speedup calculations. \n\nAny ideas? I tried googling it but couldn't come up with a solution :/ thanks in advance",
      "votes": null
    },
    {
      "id": "579317",
      "postDate": "07/18/2019 18:25:31",
      "content": "<p>Hello <a href=\"/felipemello\">@felipemello</a>  </p>\n\n<p>Check if the below example can inspire you for a solution to your need ..</p>\n\n<p>In the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2].</p>\n\n<p>Example 2\n Live Demo\nimport numpy as np \nx = np.array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]]) </p>\n\n<p>print 'Our array is:' \nprint x \nprint '\\n' </p>\n\n<p>rows = np.array([[0,0],[3,3]])\ncols = np.array([[0,2],[0,2]]) \ny = x[rows,cols] </p>\n\n<p>print 'The corner elements of this array are:' \nprint y</p>\n\n<p>The output of this program is as follows −</p>\n\n<p>Our array is: <br>\n[[ 0  1  2] <br>\n [ 3  4  5] <br>\n [ 6  7  8] <br>\n [ 9 10 11]]</p>\n\n<p>The corner elements of this array are: <br>\n[[ 0  2] <br>\n [ 9 11]] </p>\n\n<p><a href=\"https://www.tutorialspoint.com/numpy/numpy_advanced_indexing\">https://www.tutorialspoint.com/numpy/numpy_advanced_indexing</a></p>",
      "rawMarkdown": "Hello @felipemello  \n\nCheck if the below example can inspire you for a solution to your need ..\n\nIn the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2].\n\nExample 2\n Live Demo\nimport numpy as np \nx = np.array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]]) \n   \nprint 'Our array is:' \nprint x \nprint '\\n' \n\nrows = np.array([[0,0],[3,3]])\ncols = np.array([[0,2],[0,2]]) \ny = x[rows,cols] \n   \nprint 'The corner elements of this array are:' \nprint y\n\n\n\nThe output of this program is as follows −\n\nOur array is:                                                                 \n[[ 0  1  2]                                                                   \n [ 3  4  5]                                                                   \n [ 6  7  8]                                                                   \n [ 9 10 11]]\n \nThe corner elements of this array are:                                        \n[[ 0  2]                                                                      \n [ 9 11]] \n\n\n\nhttps://www.tutorialspoint.com/numpy/numpy_advanced_indexing",
      "votes": null
    },
    {
      "id": "579329",
      "postDate": "07/18/2019 18:38:56",
      "content": "<p>Not sure if its the fastest or the most pythonic way, but it worked :) Thanks!</p>\n\n<p>ind_per_row = 5\nrows = [np.zeros(ind_per_row, dtype = np.int) + i for i in range(len(matrix))]\ncols = ind</p>\n\n<p>result = matrix[rows, cols]</p>\n\n<p>print(rows)\n[array([0, 0, 0, 0, 0]),\n array([1, 1, 1, 1, 1]),\n array([2, 2, 2, 2, 2]),\n array([3, 3, 3, 3, 3]),\n array([4, 4, 4, 4, 4]),\n array([5, 5, 5, 5, 5]),\n array([6, 6, 6, 6, 6]),\n array([7, 7, 7, 7, 7]),\n array([8, 8, 8, 8, 8]),\n array([9, 9, 9, 9, 9])]</p>\n\n<p>print(cols)\narray([[5, 6, 7, 1, 4],\n       [8, 2, 3, 0, 5],\n       [9, 1, 8, 3, 5],\n       [4, 1, 8, 2, 5],\n       [3, 1, 8, 2, 5],\n       [0, 7, 6, 1, 4],\n       [0, 7, 5, 1, 3],\n       [0, 6, 5, 1, 3],\n       [1, 2, 3, 0, 5],\n       [2, 1, 7, 3, 5]])</p>",
      "rawMarkdown": "Not sure if its the fastest or the most pythonic way, but it worked :) Thanks!\n\nind_per_row = 5\nrows = [np.zeros(ind_per_row, dtype = np.int) + i for i in range(len(matrix))]\ncols = ind\n\nresult = matrix[rows, cols]\n\nprint(rows)\n[array([0, 0, 0, 0, 0]),\n array([1, 1, 1, 1, 1]),\n array([2, 2, 2, 2, 2]),\n array([3, 3, 3, 3, 3]),\n array([4, 4, 4, 4, 4]),\n array([5, 5, 5, 5, 5]),\n array([6, 6, 6, 6, 6]),\n array([7, 7, 7, 7, 7]),\n array([8, 8, 8, 8, 8]),\n array([9, 9, 9, 9, 9])]\n\nprint(cols)\narray([[5, 6, 7, 1, 4],\n       [8, 2, 3, 0, 5],\n       [9, 1, 8, 3, 5],\n       [4, 1, 8, 2, 5],\n       [3, 1, 8, 2, 5],\n       [0, 7, 6, 1, 4],\n       [0, 7, 5, 1, 3],\n       [0, 6, 5, 1, 3],\n       [1, 2, 3, 0, 5],\n       [2, 1, 7, 3, 5]])",
      "votes": null
    },
    {
      "id": "579344",
      "postDate": "07/18/2019 18:48:26",
      "content": "<p>👍 </p>",
      "rawMarkdown": "👍",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 579317,
      "author_name": "prmohanty",
      "author_url": "",
      "post_date": "07/18/2019 18:25:31",
      "content": "<p>Hello <a href=\"/felipemello\">@felipemello</a>  </p>\n\n<p>Check if the below example can inspire you for a solution to your need ..</p>\n\n<p>In the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2].</p>\n\n<p>Example 2\n Live Demo\nimport numpy as np \nx = np.array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]]) </p>\n\n<p>print 'Our array is:' \nprint x \nprint '\\n' </p>\n\n<p>rows = np.array([[0,0],[3,3]])\ncols = np.array([[0,2],[0,2]]) \ny = x[rows,cols] </p>\n\n<p>print 'The corner elements of this array are:' \nprint y</p>\n\n<p>The output of this program is as follows −</p>\n\n<p>Our array is: <br>\n[[ 0  1  2] <br>\n [ 3  4  5] <br>\n [ 6  7  8] <br>\n [ 9 10 11]]</p>\n\n<p>The corner elements of this array are: <br>\n[[ 0  2] <br>\n [ 9 11]] </p>\n\n<p><a href=\"https://www.tutorialspoint.com/numpy/numpy_advanced_indexing\">https://www.tutorialspoint.com/numpy/numpy_advanced_indexing</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 579329,
          "author_name": "felipemello",
          "author_url": "",
          "post_date": "07/18/2019 18:38:56",
          "content": "<p>Not sure if its the fastest or the most pythonic way, but it worked :) Thanks!</p>\n\n<p>ind_per_row = 5\nrows = [np.zeros(ind_per_row, dtype = np.int) + i for i in range(len(matrix))]\ncols = ind</p>\n\n<p>result = matrix[rows, cols]</p>\n\n<p>print(rows)\n[array([0, 0, 0, 0, 0]),\n array([1, 1, 1, 1, 1]),\n array([2, 2, 2, 2, 2]),\n array([3, 3, 3, 3, 3]),\n array([4, 4, 4, 4, 4]),\n array([5, 5, 5, 5, 5]),\n array([6, 6, 6, 6, 6]),\n array([7, 7, 7, 7, 7]),\n array([8, 8, 8, 8, 8]),\n array([9, 9, 9, 9, 9])]</p>\n\n<p>print(cols)\narray([[5, 6, 7, 1, 4],\n       [8, 2, 3, 0, 5],\n       [9, 1, 8, 3, 5],\n       [4, 1, 8, 2, 5],\n       [3, 1, 8, 2, 5],\n       [0, 7, 6, 1, 4],\n       [0, 7, 5, 1, 3],\n       [0, 6, 5, 1, 3],\n       [1, 2, 3, 0, 5],\n       [2, 1, 7, 3, 5]])</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 579344,
          "author_name": "prmohanty",
          "author_url": "",
          "post_date": "07/18/2019 18:48:26",
          "content": "<p>👍 </p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "579310": "Hi, can someone help me with this numpy question?\n\nI have a (10,10) atom distance matrix like this:\n\na = array([[ 0,  2,  6,  6, 12,  1,  1,  1,  4,  6],\n       [ 2,  0,  2,  2,  6,  4,  4,  4,  1,  3],\n       [ 6,  2,  0,  5, 11,  7, 11,  7,  4,  0],\n       [ 6,  2,  5,  0,  1, 11,  7,  7,  4,  6],\n       [12,  6, 11,  1,  0, 20, 12, 12,  9, 11],\n       [ 1,  4,  7, 11, 20,  0,  3,  3,  6,  8],\n       [ 1,  4, 11,  7, 12,  3,  0,  3,  6, 13],\n       [ 1,  4,  7,  7, 12,  3,  3,  0,  9,  6],\n       [ 4,  1,  4,  4,  9,  6,  6,  9,  0,  7],\n       [ 6,  3,  0,  6, 11,  8, 13,  6,  7,  0]])\n\nI want to access 5 indices of each array. The indices are:\n\nind = array([[1, 2, 3, 4, 5],\n       [1, 2, 3, 4, 5],\n       [9, 1, 8, 3, 5],\n       [4, 1, 8, 2, 5],\n       [3, 1, 8, 2, 5],\n       [0, 7, 6, 1, 4],\n       [0, 7, 5, 1, 3],\n       [0, 6, 5, 1, 3],\n       [1, 2, 3, 0, 5],\n       [2, 1, 7, 3, 5]])\n\nFor a[0] I want a[0][ind[0]]\nFor a[1] I want a[1][ind[1]]\n\nBut I wanted to do it in parallel, without needing a for loop or something like that, to speedup calculations. \n\nAny ideas? I tried googling it but couldn't come up with a solution :/ thanks in advance",
    "579317": "Hello @felipemello  \n\nCheck if the below example can inspire you for a solution to your need ..\n\nIn the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2].\n\nExample 2\n Live Demo\nimport numpy as np \nx = np.array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]]) \n   \nprint 'Our array is:' \nprint x \nprint '\\n' \n\nrows = np.array([[0,0],[3,3]])\ncols = np.array([[0,2],[0,2]]) \ny = x[rows,cols] \n   \nprint 'The corner elements of this array are:' \nprint y\n\n\n\nThe output of this program is as follows −\n\nOur array is:                                                                 \n[[ 0  1  2]                                                                   \n [ 3  4  5]                                                                   \n [ 6  7  8]                                                                   \n [ 9 10 11]]\n \nThe corner elements of this array are:                                        \n[[ 0  2]                                                                      \n [ 9 11]] \n\n\n\nhttps://www.tutorialspoint.com/numpy/numpy_advanced_indexing",
    "579329": "Not sure if its the fastest or the most pythonic way, but it worked :) Thanks!\n\nind_per_row = 5\nrows = [np.zeros(ind_per_row, dtype = np.int) + i for i in range(len(matrix))]\ncols = ind\n\nresult = matrix[rows, cols]\n\nprint(rows)\n[array([0, 0, 0, 0, 0]),\n array([1, 1, 1, 1, 1]),\n array([2, 2, 2, 2, 2]),\n array([3, 3, 3, 3, 3]),\n array([4, 4, 4, 4, 4]),\n array([5, 5, 5, 5, 5]),\n array([6, 6, 6, 6, 6]),\n array([7, 7, 7, 7, 7]),\n array([8, 8, 8, 8, 8]),\n array([9, 9, 9, 9, 9])]\n\nprint(cols)\narray([[5, 6, 7, 1, 4],\n       [8, 2, 3, 0, 5],\n       [9, 1, 8, 3, 5],\n       [4, 1, 8, 2, 5],\n       [3, 1, 8, 2, 5],\n       [0, 7, 6, 1, 4],\n       [0, 7, 5, 1, 3],\n       [0, 6, 5, 1, 3],\n       [1, 2, 3, 0, 5],\n       [2, 1, 7, 3, 5]])",
    "579344": "👍"
  },
  "source": "meta"
}