Square array from linear array python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I would like to get a square matrix B from a linear vector A such that B = A * transpose(A). A is a numpy array and np.shape(A) returns (10,). I would like B to be a (10,10) array. I tried B = np.matmut(A, A[np.newaxis]) but I get an error :




shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)










share|improve this question

























  • What I want is the equivalent of "B=A*ctranspose(A)" in matlab

    – Mathieu Lecoq
    Nov 23 '18 at 14:12


















1















I would like to get a square matrix B from a linear vector A such that B = A * transpose(A). A is a numpy array and np.shape(A) returns (10,). I would like B to be a (10,10) array. I tried B = np.matmut(A, A[np.newaxis]) but I get an error :




shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)










share|improve this question

























  • What I want is the equivalent of "B=A*ctranspose(A)" in matlab

    – Mathieu Lecoq
    Nov 23 '18 at 14:12














1












1








1








I would like to get a square matrix B from a linear vector A such that B = A * transpose(A). A is a numpy array and np.shape(A) returns (10,). I would like B to be a (10,10) array. I tried B = np.matmut(A, A[np.newaxis]) but I get an error :




shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)










share|improve this question
















I would like to get a square matrix B from a linear vector A such that B = A * transpose(A). A is a numpy array and np.shape(A) returns (10,). I would like B to be a (10,10) array. I tried B = np.matmut(A, A[np.newaxis]) but I get an error :




shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)







python arrays numpy matrix vector






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 19:16









MarianD

4,46761432




4,46761432










asked Nov 23 '18 at 14:08









Mathieu LecoqMathieu Lecoq

83




83













  • What I want is the equivalent of "B=A*ctranspose(A)" in matlab

    – Mathieu Lecoq
    Nov 23 '18 at 14:12



















  • What I want is the equivalent of "B=A*ctranspose(A)" in matlab

    – Mathieu Lecoq
    Nov 23 '18 at 14:12

















What I want is the equivalent of "B=A*ctranspose(A)" in matlab

– Mathieu Lecoq
Nov 23 '18 at 14:12





What I want is the equivalent of "B=A*ctranspose(A)" in matlab

– Mathieu Lecoq
Nov 23 '18 at 14:12












3 Answers
3






active

oldest

votes


















3














you can do this using outer:



import numpy as np
vector = np.arange(10)
np.outer(vector, vector)





share|improve this answer































    2














    The solution is a little ugly, but it does what you need.



    import numpy as np

    vector = np.array([1,2,3,4,5,6,7,8,9,10],)
    matrix = np.dot(vector[:,None],vector[None,:])
    print(matrix)


    You can also do the following:



    import numpy as np

    vector = np.array([1,2,3,4,5,6,7,8,9,10],)
    matrix = vector*vector[:,None]
    print(matrix)


    The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.






    share|improve this answer































      0














      Variation on outer product:



      a = A.reshape(-1, 1) # make sure it's a column vector
      B = a @ a.T





      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%2f53448202%2fsquare-array-from-linear-array-python%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        you can do this using outer:



        import numpy as np
        vector = np.arange(10)
        np.outer(vector, vector)





        share|improve this answer




























          3














          you can do this using outer:



          import numpy as np
          vector = np.arange(10)
          np.outer(vector, vector)





          share|improve this answer


























            3












            3








            3







            you can do this using outer:



            import numpy as np
            vector = np.arange(10)
            np.outer(vector, vector)





            share|improve this answer













            you can do this using outer:



            import numpy as np
            vector = np.arange(10)
            np.outer(vector, vector)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 14:20









            jeremycgjeremycg

            19.1k44257




            19.1k44257

























                2














                The solution is a little ugly, but it does what you need.



                import numpy as np

                vector = np.array([1,2,3,4,5,6,7,8,9,10],)
                matrix = np.dot(vector[:,None],vector[None,:])
                print(matrix)


                You can also do the following:



                import numpy as np

                vector = np.array([1,2,3,4,5,6,7,8,9,10],)
                matrix = vector*vector[:,None]
                print(matrix)


                The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.






                share|improve this answer




























                  2














                  The solution is a little ugly, but it does what you need.



                  import numpy as np

                  vector = np.array([1,2,3,4,5,6,7,8,9,10],)
                  matrix = np.dot(vector[:,None],vector[None,:])
                  print(matrix)


                  You can also do the following:



                  import numpy as np

                  vector = np.array([1,2,3,4,5,6,7,8,9,10],)
                  matrix = vector*vector[:,None]
                  print(matrix)


                  The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.






                  share|improve this answer


























                    2












                    2








                    2







                    The solution is a little ugly, but it does what you need.



                    import numpy as np

                    vector = np.array([1,2,3,4,5,6,7,8,9,10],)
                    matrix = np.dot(vector[:,None],vector[None,:])
                    print(matrix)


                    You can also do the following:



                    import numpy as np

                    vector = np.array([1,2,3,4,5,6,7,8,9,10],)
                    matrix = vector*vector[:,None]
                    print(matrix)


                    The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.






                    share|improve this answer













                    The solution is a little ugly, but it does what you need.



                    import numpy as np

                    vector = np.array([1,2,3,4,5,6,7,8,9,10],)
                    matrix = np.dot(vector[:,None],vector[None,:])
                    print(matrix)


                    You can also do the following:



                    import numpy as np

                    vector = np.array([1,2,3,4,5,6,7,8,9,10],)
                    matrix = vector*vector[:,None]
                    print(matrix)


                    The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 14:19









                    Esteban QuirosEsteban Quiros

                    1015




                    1015























                        0














                        Variation on outer product:



                        a = A.reshape(-1, 1) # make sure it's a column vector
                        B = a @ a.T





                        share|improve this answer




























                          0














                          Variation on outer product:



                          a = A.reshape(-1, 1) # make sure it's a column vector
                          B = a @ a.T





                          share|improve this answer


























                            0












                            0








                            0







                            Variation on outer product:



                            a = A.reshape(-1, 1) # make sure it's a column vector
                            B = a @ a.T





                            share|improve this answer













                            Variation on outer product:



                            a = A.reshape(-1, 1) # make sure it's a column vector
                            B = a @ a.T






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 23 '18 at 15:32









                            deckarddeckard

                            31527




                            31527






























                                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%2f53448202%2fsquare-array-from-linear-array-python%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

                                "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                Alcedinidae

                                RAC Tourist Trophy