Index a 3D array with 2D array numpyPython: Access saved points from 2d array in 3d numpy arrayCreate ArrayList from arrayFinding the index of an item given a list containing it in PythonHow do I check if an array includes an object in JavaScript?How to append something to an array?Accessing the index in 'for' loops?How to insert an item into an array at a specific index (JavaScript)?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Circuit Analysis: Obtaining Close Loop OP - AMP Transfer function

Quoting Keynes in a lecture

What is the highest possible scrabble score for placing a single tile

What does Apple's new App Store requirement mean

When were female captains banned from Starfleet?

A Trivial Diagnosis

Has the laser at Magurele, Romania reached a tenth of the Sun's power?

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

Is there a RAID 0 Equivalent for RAM?

Which was the first story featuring espers?

How much of a Devil Fruit must be consumed to gain the power?

Which Article Helped Get Rid of Technobabble in RPGs?

Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?

"It doesn't matter" or "it won't matter"?

Non-trope happy ending?

Is my low blitz game drawing rate at www.chess.com an indicator that I am weak in chess?

Is it necessary to use pronouns with the verb "essere"?

Can you use Vicious Mockery to win an argument or gain favours?

Why is the "ls" command showing permissions of files in a FAT32 partition?

What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?

How can I write humor as character trait?

C++ check if statement can be evaluated constexpr

How could a planet have erratic days?

What kind of floor tile is this?



Index a 3D array with 2D array numpy


Python: Access saved points from 2d array in 3d numpy arrayCreate ArrayList from arrayFinding the index of an item given a list containing it in PythonHow do I check if an array includes an object in JavaScript?How to append something to an array?Accessing the index in 'for' loops?How to insert an item into an array at a specific index (JavaScript)?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?













0















I'm trying to manipulate an index and source array such that:



result[ i ][ j ][ k ] = source[ i ][ indices[ i ][ j ][ k ] ]



I know how to do this with for loops but I'm using giant arrays and I'd like to use something more time efficient. I've tried to use numpy's advanced indexing but I don't really understand it.



Example functionality:



source = [[0.0 0.1 0.2 0.3]
[1.0 1.1 1.2 1.3]
[2.0 2.1 2.2 2.3]]

indices = [[[3 1 0 1]
[3 0 0 3]]

[[0 1 0 2]
[3 2 1 1]]

[[1 1 0 1]
[0 1 2 2]]]

# result[i][j][k] = source[i][indices[i][j][k]]

result = [[[0.3 0.1 0.0 0.1]
[0.3 0.0 0.0 0.3]]

[[1.0 1.1 1.0 1.2]
[1.3 1.2 1.1 1.1]]

[[2.1 2.1 2.0 2.1]
[2.0 2.1 2.2 2.2]]]









share|improve this question






















  • You need idx0 = np.r_['0,3,0', :3]. which is the same as np.arange(3).reshape(3,1,1) Then result = source[idx0, indices]

    – Paul Panzer
    Mar 8 at 0:14
















0















I'm trying to manipulate an index and source array such that:



result[ i ][ j ][ k ] = source[ i ][ indices[ i ][ j ][ k ] ]



I know how to do this with for loops but I'm using giant arrays and I'd like to use something more time efficient. I've tried to use numpy's advanced indexing but I don't really understand it.



Example functionality:



source = [[0.0 0.1 0.2 0.3]
[1.0 1.1 1.2 1.3]
[2.0 2.1 2.2 2.3]]

indices = [[[3 1 0 1]
[3 0 0 3]]

[[0 1 0 2]
[3 2 1 1]]

[[1 1 0 1]
[0 1 2 2]]]

# result[i][j][k] = source[i][indices[i][j][k]]

result = [[[0.3 0.1 0.0 0.1]
[0.3 0.0 0.0 0.3]]

[[1.0 1.1 1.0 1.2]
[1.3 1.2 1.1 1.1]]

[[2.1 2.1 2.0 2.1]
[2.0 2.1 2.2 2.2]]]









share|improve this question






















  • You need idx0 = np.r_['0,3,0', :3]. which is the same as np.arange(3).reshape(3,1,1) Then result = source[idx0, indices]

    – Paul Panzer
    Mar 8 at 0:14














0












0








0








I'm trying to manipulate an index and source array such that:



result[ i ][ j ][ k ] = source[ i ][ indices[ i ][ j ][ k ] ]



I know how to do this with for loops but I'm using giant arrays and I'd like to use something more time efficient. I've tried to use numpy's advanced indexing but I don't really understand it.



Example functionality:



source = [[0.0 0.1 0.2 0.3]
[1.0 1.1 1.2 1.3]
[2.0 2.1 2.2 2.3]]

indices = [[[3 1 0 1]
[3 0 0 3]]

[[0 1 0 2]
[3 2 1 1]]

[[1 1 0 1]
[0 1 2 2]]]

# result[i][j][k] = source[i][indices[i][j][k]]

result = [[[0.3 0.1 0.0 0.1]
[0.3 0.0 0.0 0.3]]

[[1.0 1.1 1.0 1.2]
[1.3 1.2 1.1 1.1]]

[[2.1 2.1 2.0 2.1]
[2.0 2.1 2.2 2.2]]]









share|improve this question














I'm trying to manipulate an index and source array such that:



result[ i ][ j ][ k ] = source[ i ][ indices[ i ][ j ][ k ] ]



I know how to do this with for loops but I'm using giant arrays and I'd like to use something more time efficient. I've tried to use numpy's advanced indexing but I don't really understand it.



Example functionality:



source = [[0.0 0.1 0.2 0.3]
[1.0 1.1 1.2 1.3]
[2.0 2.1 2.2 2.3]]

indices = [[[3 1 0 1]
[3 0 0 3]]

[[0 1 0 2]
[3 2 1 1]]

[[1 1 0 1]
[0 1 2 2]]]

# result[i][j][k] = source[i][indices[i][j][k]]

result = [[[0.3 0.1 0.0 0.1]
[0.3 0.0 0.0 0.3]]

[[1.0 1.1 1.0 1.2]
[1.3 1.2 1.1 1.1]]

[[2.1 2.1 2.0 2.1]
[2.0 2.1 2.2 2.2]]]






python arrays numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 23:44









Darci PDarci P

31




31












  • You need idx0 = np.r_['0,3,0', :3]. which is the same as np.arange(3).reshape(3,1,1) Then result = source[idx0, indices]

    – Paul Panzer
    Mar 8 at 0:14


















  • You need idx0 = np.r_['0,3,0', :3]. which is the same as np.arange(3).reshape(3,1,1) Then result = source[idx0, indices]

    – Paul Panzer
    Mar 8 at 0:14

















You need idx0 = np.r_['0,3,0', :3]. which is the same as np.arange(3).reshape(3,1,1) Then result = source[idx0, indices]

– Paul Panzer
Mar 8 at 0:14






You need idx0 = np.r_['0,3,0', :3]. which is the same as np.arange(3).reshape(3,1,1) Then result = source[idx0, indices]

– Paul Panzer
Mar 8 at 0:14













1 Answer
1






active

oldest

votes


















0














Solution using Integer Advanced Indexing:



Given:



source = [[0.0, 0.1, 0.2, 0.3],
[1.0, 1.1, 1.2, 1.3],
[2.0, 2.1, 2.2, 2.3]]

indices = [[[3, 1, 0, 1],
[3, 0, 0, 3]],
[[0, 1, 0, 2],
[3, 2, 1, 1]],
[[1, 1, 0, 1],
[0, 1, 2, 2]]]


Use this:



import numpy as np
nd_source = np.array(source)

source_rows = len(source) # == 3, in above example
source_cols = len(source[0]) # == 4, in above example

row_indices = np.arange(source_rows).reshape(-1,1,1)
result = nd_source [row_indices, indices]


Result:



print (result)
[[[0.3 0.1 0. 0.1]
[0.3 0. 0. 0.3]]

[[1. 1.1 1. 1.2]
[1.3 1.2 1.1 1.1]]

[[2.1 2.1 2. 2.1]
[2. 2.1 2.2 2.2]]]


Explanation:



To use Integer Advanced Indexing, the key rules are:



  1. We must supply index arrays consisting of integer indices.

  2. We must supply as many of these index arrays, as there are dimensions in the source array.

  3. The shape of these index arrays must be the same, or, at least all of them must be broadcastable to a single final shape.

How the Integer Advanced Indexing works is:



Given that source array has n dimensions, and that we have therefore supplied n integer index arrays:



  1. All of these index arrays, if not in the same uniform shape, will be broadcasted to be in a single uniform shape.

  2. To access any element in the source array, we obviously need an n-tuple of indices. Therefore to generate the result array from the source array, we need several n-tuples, one n-tuple for each element-position of the final result array. For each element-position of the result array, the n-tuple of indices will be constructed from the corresponding element-positions in the broadcasted index arrays. (Remember the result array has exactly the same shape as the broadcasted index arrays, as already mentioned above).

  3. Thus, by traversing the index arrays in tandem, we get all the n-tuples we need to generate the result array, in the same shape as the broadcasted index arrays.

Applying this explanation to the above example:



  1. Our source array is nd_source = np.array(source), which is 2d.

  2. Our final result shape is (3,2,4).


  3. We therefore need to supply 2 index arrays, and these index arrays must either be in the final result shape of (3,2,4), or broadcastable to the (3,2,4) shape.


  4. Our first index array is row_indices = np.arange(source_rows).reshape(-1,1,1). (source_rows is the number of rows in the source, which is 3 in this example) This index array has shape (3,1,1), and actually looks like [[[0]],[[1]],[[2]]]. This is broadcastable to the final result shape of (3,2,4), and the broadcasted array looks like [[[0,0,0,0],[0,0,0,0]],[[1,1,1,1],[1,1,1,1]],[[2,2,2,2],[2,2,2,2]]].


  5. Our second index array is indices. Though this is not an array and is only a list of lists, numpy is flexible enough to automatically convert it into the corresponding ndarray, when we pass it as our send index array. Note that this array is already in the final desired result shape of (3,2,4) even without any broadcasting.


  6. Traversing these two index arrays in tandem (one a broadcasted array and the other as is), numpy generates all the 2-tuples needed to access our source 2d array nd_source, and generate the final result in the shape (3,2,4).






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054603%2findex-a-3d-array-with-2d-array-numpy%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Solution using Integer Advanced Indexing:



    Given:



    source = [[0.0, 0.1, 0.2, 0.3],
    [1.0, 1.1, 1.2, 1.3],
    [2.0, 2.1, 2.2, 2.3]]

    indices = [[[3, 1, 0, 1],
    [3, 0, 0, 3]],
    [[0, 1, 0, 2],
    [3, 2, 1, 1]],
    [[1, 1, 0, 1],
    [0, 1, 2, 2]]]


    Use this:



    import numpy as np
    nd_source = np.array(source)

    source_rows = len(source) # == 3, in above example
    source_cols = len(source[0]) # == 4, in above example

    row_indices = np.arange(source_rows).reshape(-1,1,1)
    result = nd_source [row_indices, indices]


    Result:



    print (result)
    [[[0.3 0.1 0. 0.1]
    [0.3 0. 0. 0.3]]

    [[1. 1.1 1. 1.2]
    [1.3 1.2 1.1 1.1]]

    [[2.1 2.1 2. 2.1]
    [2. 2.1 2.2 2.2]]]


    Explanation:



    To use Integer Advanced Indexing, the key rules are:



    1. We must supply index arrays consisting of integer indices.

    2. We must supply as many of these index arrays, as there are dimensions in the source array.

    3. The shape of these index arrays must be the same, or, at least all of them must be broadcastable to a single final shape.

    How the Integer Advanced Indexing works is:



    Given that source array has n dimensions, and that we have therefore supplied n integer index arrays:



    1. All of these index arrays, if not in the same uniform shape, will be broadcasted to be in a single uniform shape.

    2. To access any element in the source array, we obviously need an n-tuple of indices. Therefore to generate the result array from the source array, we need several n-tuples, one n-tuple for each element-position of the final result array. For each element-position of the result array, the n-tuple of indices will be constructed from the corresponding element-positions in the broadcasted index arrays. (Remember the result array has exactly the same shape as the broadcasted index arrays, as already mentioned above).

    3. Thus, by traversing the index arrays in tandem, we get all the n-tuples we need to generate the result array, in the same shape as the broadcasted index arrays.

    Applying this explanation to the above example:



    1. Our source array is nd_source = np.array(source), which is 2d.

    2. Our final result shape is (3,2,4).


    3. We therefore need to supply 2 index arrays, and these index arrays must either be in the final result shape of (3,2,4), or broadcastable to the (3,2,4) shape.


    4. Our first index array is row_indices = np.arange(source_rows).reshape(-1,1,1). (source_rows is the number of rows in the source, which is 3 in this example) This index array has shape (3,1,1), and actually looks like [[[0]],[[1]],[[2]]]. This is broadcastable to the final result shape of (3,2,4), and the broadcasted array looks like [[[0,0,0,0],[0,0,0,0]],[[1,1,1,1],[1,1,1,1]],[[2,2,2,2],[2,2,2,2]]].


    5. Our second index array is indices. Though this is not an array and is only a list of lists, numpy is flexible enough to automatically convert it into the corresponding ndarray, when we pass it as our send index array. Note that this array is already in the final desired result shape of (3,2,4) even without any broadcasting.


    6. Traversing these two index arrays in tandem (one a broadcasted array and the other as is), numpy generates all the 2-tuples needed to access our source 2d array nd_source, and generate the final result in the shape (3,2,4).






    share|improve this answer





























      0














      Solution using Integer Advanced Indexing:



      Given:



      source = [[0.0, 0.1, 0.2, 0.3],
      [1.0, 1.1, 1.2, 1.3],
      [2.0, 2.1, 2.2, 2.3]]

      indices = [[[3, 1, 0, 1],
      [3, 0, 0, 3]],
      [[0, 1, 0, 2],
      [3, 2, 1, 1]],
      [[1, 1, 0, 1],
      [0, 1, 2, 2]]]


      Use this:



      import numpy as np
      nd_source = np.array(source)

      source_rows = len(source) # == 3, in above example
      source_cols = len(source[0]) # == 4, in above example

      row_indices = np.arange(source_rows).reshape(-1,1,1)
      result = nd_source [row_indices, indices]


      Result:



      print (result)
      [[[0.3 0.1 0. 0.1]
      [0.3 0. 0. 0.3]]

      [[1. 1.1 1. 1.2]
      [1.3 1.2 1.1 1.1]]

      [[2.1 2.1 2. 2.1]
      [2. 2.1 2.2 2.2]]]


      Explanation:



      To use Integer Advanced Indexing, the key rules are:



      1. We must supply index arrays consisting of integer indices.

      2. We must supply as many of these index arrays, as there are dimensions in the source array.

      3. The shape of these index arrays must be the same, or, at least all of them must be broadcastable to a single final shape.

      How the Integer Advanced Indexing works is:



      Given that source array has n dimensions, and that we have therefore supplied n integer index arrays:



      1. All of these index arrays, if not in the same uniform shape, will be broadcasted to be in a single uniform shape.

      2. To access any element in the source array, we obviously need an n-tuple of indices. Therefore to generate the result array from the source array, we need several n-tuples, one n-tuple for each element-position of the final result array. For each element-position of the result array, the n-tuple of indices will be constructed from the corresponding element-positions in the broadcasted index arrays. (Remember the result array has exactly the same shape as the broadcasted index arrays, as already mentioned above).

      3. Thus, by traversing the index arrays in tandem, we get all the n-tuples we need to generate the result array, in the same shape as the broadcasted index arrays.

      Applying this explanation to the above example:



      1. Our source array is nd_source = np.array(source), which is 2d.

      2. Our final result shape is (3,2,4).


      3. We therefore need to supply 2 index arrays, and these index arrays must either be in the final result shape of (3,2,4), or broadcastable to the (3,2,4) shape.


      4. Our first index array is row_indices = np.arange(source_rows).reshape(-1,1,1). (source_rows is the number of rows in the source, which is 3 in this example) This index array has shape (3,1,1), and actually looks like [[[0]],[[1]],[[2]]]. This is broadcastable to the final result shape of (3,2,4), and the broadcasted array looks like [[[0,0,0,0],[0,0,0,0]],[[1,1,1,1],[1,1,1,1]],[[2,2,2,2],[2,2,2,2]]].


      5. Our second index array is indices. Though this is not an array and is only a list of lists, numpy is flexible enough to automatically convert it into the corresponding ndarray, when we pass it as our send index array. Note that this array is already in the final desired result shape of (3,2,4) even without any broadcasting.


      6. Traversing these two index arrays in tandem (one a broadcasted array and the other as is), numpy generates all the 2-tuples needed to access our source 2d array nd_source, and generate the final result in the shape (3,2,4).






      share|improve this answer



























        0












        0








        0







        Solution using Integer Advanced Indexing:



        Given:



        source = [[0.0, 0.1, 0.2, 0.3],
        [1.0, 1.1, 1.2, 1.3],
        [2.0, 2.1, 2.2, 2.3]]

        indices = [[[3, 1, 0, 1],
        [3, 0, 0, 3]],
        [[0, 1, 0, 2],
        [3, 2, 1, 1]],
        [[1, 1, 0, 1],
        [0, 1, 2, 2]]]


        Use this:



        import numpy as np
        nd_source = np.array(source)

        source_rows = len(source) # == 3, in above example
        source_cols = len(source[0]) # == 4, in above example

        row_indices = np.arange(source_rows).reshape(-1,1,1)
        result = nd_source [row_indices, indices]


        Result:



        print (result)
        [[[0.3 0.1 0. 0.1]
        [0.3 0. 0. 0.3]]

        [[1. 1.1 1. 1.2]
        [1.3 1.2 1.1 1.1]]

        [[2.1 2.1 2. 2.1]
        [2. 2.1 2.2 2.2]]]


        Explanation:



        To use Integer Advanced Indexing, the key rules are:



        1. We must supply index arrays consisting of integer indices.

        2. We must supply as many of these index arrays, as there are dimensions in the source array.

        3. The shape of these index arrays must be the same, or, at least all of them must be broadcastable to a single final shape.

        How the Integer Advanced Indexing works is:



        Given that source array has n dimensions, and that we have therefore supplied n integer index arrays:



        1. All of these index arrays, if not in the same uniform shape, will be broadcasted to be in a single uniform shape.

        2. To access any element in the source array, we obviously need an n-tuple of indices. Therefore to generate the result array from the source array, we need several n-tuples, one n-tuple for each element-position of the final result array. For each element-position of the result array, the n-tuple of indices will be constructed from the corresponding element-positions in the broadcasted index arrays. (Remember the result array has exactly the same shape as the broadcasted index arrays, as already mentioned above).

        3. Thus, by traversing the index arrays in tandem, we get all the n-tuples we need to generate the result array, in the same shape as the broadcasted index arrays.

        Applying this explanation to the above example:



        1. Our source array is nd_source = np.array(source), which is 2d.

        2. Our final result shape is (3,2,4).


        3. We therefore need to supply 2 index arrays, and these index arrays must either be in the final result shape of (3,2,4), or broadcastable to the (3,2,4) shape.


        4. Our first index array is row_indices = np.arange(source_rows).reshape(-1,1,1). (source_rows is the number of rows in the source, which is 3 in this example) This index array has shape (3,1,1), and actually looks like [[[0]],[[1]],[[2]]]. This is broadcastable to the final result shape of (3,2,4), and the broadcasted array looks like [[[0,0,0,0],[0,0,0,0]],[[1,1,1,1],[1,1,1,1]],[[2,2,2,2],[2,2,2,2]]].


        5. Our second index array is indices. Though this is not an array and is only a list of lists, numpy is flexible enough to automatically convert it into the corresponding ndarray, when we pass it as our send index array. Note that this array is already in the final desired result shape of (3,2,4) even without any broadcasting.


        6. Traversing these two index arrays in tandem (one a broadcasted array and the other as is), numpy generates all the 2-tuples needed to access our source 2d array nd_source, and generate the final result in the shape (3,2,4).






        share|improve this answer















        Solution using Integer Advanced Indexing:



        Given:



        source = [[0.0, 0.1, 0.2, 0.3],
        [1.0, 1.1, 1.2, 1.3],
        [2.0, 2.1, 2.2, 2.3]]

        indices = [[[3, 1, 0, 1],
        [3, 0, 0, 3]],
        [[0, 1, 0, 2],
        [3, 2, 1, 1]],
        [[1, 1, 0, 1],
        [0, 1, 2, 2]]]


        Use this:



        import numpy as np
        nd_source = np.array(source)

        source_rows = len(source) # == 3, in above example
        source_cols = len(source[0]) # == 4, in above example

        row_indices = np.arange(source_rows).reshape(-1,1,1)
        result = nd_source [row_indices, indices]


        Result:



        print (result)
        [[[0.3 0.1 0. 0.1]
        [0.3 0. 0. 0.3]]

        [[1. 1.1 1. 1.2]
        [1.3 1.2 1.1 1.1]]

        [[2.1 2.1 2. 2.1]
        [2. 2.1 2.2 2.2]]]


        Explanation:



        To use Integer Advanced Indexing, the key rules are:



        1. We must supply index arrays consisting of integer indices.

        2. We must supply as many of these index arrays, as there are dimensions in the source array.

        3. The shape of these index arrays must be the same, or, at least all of them must be broadcastable to a single final shape.

        How the Integer Advanced Indexing works is:



        Given that source array has n dimensions, and that we have therefore supplied n integer index arrays:



        1. All of these index arrays, if not in the same uniform shape, will be broadcasted to be in a single uniform shape.

        2. To access any element in the source array, we obviously need an n-tuple of indices. Therefore to generate the result array from the source array, we need several n-tuples, one n-tuple for each element-position of the final result array. For each element-position of the result array, the n-tuple of indices will be constructed from the corresponding element-positions in the broadcasted index arrays. (Remember the result array has exactly the same shape as the broadcasted index arrays, as already mentioned above).

        3. Thus, by traversing the index arrays in tandem, we get all the n-tuples we need to generate the result array, in the same shape as the broadcasted index arrays.

        Applying this explanation to the above example:



        1. Our source array is nd_source = np.array(source), which is 2d.

        2. Our final result shape is (3,2,4).


        3. We therefore need to supply 2 index arrays, and these index arrays must either be in the final result shape of (3,2,4), or broadcastable to the (3,2,4) shape.


        4. Our first index array is row_indices = np.arange(source_rows).reshape(-1,1,1). (source_rows is the number of rows in the source, which is 3 in this example) This index array has shape (3,1,1), and actually looks like [[[0]],[[1]],[[2]]]. This is broadcastable to the final result shape of (3,2,4), and the broadcasted array looks like [[[0,0,0,0],[0,0,0,0]],[[1,1,1,1],[1,1,1,1]],[[2,2,2,2],[2,2,2,2]]].


        5. Our second index array is indices. Though this is not an array and is only a list of lists, numpy is flexible enough to automatically convert it into the corresponding ndarray, when we pass it as our send index array. Note that this array is already in the final desired result shape of (3,2,4) even without any broadcasting.


        6. Traversing these two index arrays in tandem (one a broadcasted array and the other as is), numpy generates all the 2-tuples needed to access our source 2d array nd_source, and generate the final result in the shape (3,2,4).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 9 at 10:43

























        answered Mar 8 at 1:51









        fountainheadfountainhead

        1,140212




        1,140212





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054603%2findex-a-3d-array-with-2d-array-numpy%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229