multiply matrix with scalar












-2















i need to create a function that multiply matrix with scalar without using numpy. tthe problem here is that the function doesn't return a matrix.( so for the matrix below it needs to return [[2,4],[6,9.0],[10,84]]



def mul_mat_by_scalar(mat, alpha):

# Write the rest of the code for question 5 below here.

mat_new =

for j in mat:

for i in j:

a = (i* alpha)

mat_new.append(a)

continue

return mat_new

print mul_mat_by_scalar([[1,2], [3,4.5], [5,42]], 2)









share|improve this question




















  • 2





    I don't think your question shows enough research effort... Can you for instance write the (mathematical) definition of scalar matrix multiplication for us?

    – Rolvernew
    Nov 21 '18 at 20:51











  • And you can also talke a close llok at your variables. What is the type of a ? what does the your append do ?

    – LoneWanderer
    Nov 21 '18 at 21:14
















-2















i need to create a function that multiply matrix with scalar without using numpy. tthe problem here is that the function doesn't return a matrix.( so for the matrix below it needs to return [[2,4],[6,9.0],[10,84]]



def mul_mat_by_scalar(mat, alpha):

# Write the rest of the code for question 5 below here.

mat_new =

for j in mat:

for i in j:

a = (i* alpha)

mat_new.append(a)

continue

return mat_new

print mul_mat_by_scalar([[1,2], [3,4.5], [5,42]], 2)









share|improve this question




















  • 2





    I don't think your question shows enough research effort... Can you for instance write the (mathematical) definition of scalar matrix multiplication for us?

    – Rolvernew
    Nov 21 '18 at 20:51











  • And you can also talke a close llok at your variables. What is the type of a ? what does the your append do ?

    – LoneWanderer
    Nov 21 '18 at 21:14














-2












-2








-2








i need to create a function that multiply matrix with scalar without using numpy. tthe problem here is that the function doesn't return a matrix.( so for the matrix below it needs to return [[2,4],[6,9.0],[10,84]]



def mul_mat_by_scalar(mat, alpha):

# Write the rest of the code for question 5 below here.

mat_new =

for j in mat:

for i in j:

a = (i* alpha)

mat_new.append(a)

continue

return mat_new

print mul_mat_by_scalar([[1,2], [3,4.5], [5,42]], 2)









share|improve this question
















i need to create a function that multiply matrix with scalar without using numpy. tthe problem here is that the function doesn't return a matrix.( so for the matrix below it needs to return [[2,4],[6,9.0],[10,84]]



def mul_mat_by_scalar(mat, alpha):

# Write the rest of the code for question 5 below here.

mat_new =

for j in mat:

for i in j:

a = (i* alpha)

mat_new.append(a)

continue

return mat_new

print mul_mat_by_scalar([[1,2], [3,4.5], [5,42]], 2)






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:33







user10687882

















asked Nov 21 '18 at 20:23









user10687882user10687882

1




1








  • 2





    I don't think your question shows enough research effort... Can you for instance write the (mathematical) definition of scalar matrix multiplication for us?

    – Rolvernew
    Nov 21 '18 at 20:51











  • And you can also talke a close llok at your variables. What is the type of a ? what does the your append do ?

    – LoneWanderer
    Nov 21 '18 at 21:14














  • 2





    I don't think your question shows enough research effort... Can you for instance write the (mathematical) definition of scalar matrix multiplication for us?

    – Rolvernew
    Nov 21 '18 at 20:51











  • And you can also talke a close llok at your variables. What is the type of a ? what does the your append do ?

    – LoneWanderer
    Nov 21 '18 at 21:14








2




2





I don't think your question shows enough research effort... Can you for instance write the (mathematical) definition of scalar matrix multiplication for us?

– Rolvernew
Nov 21 '18 at 20:51





I don't think your question shows enough research effort... Can you for instance write the (mathematical) definition of scalar matrix multiplication for us?

– Rolvernew
Nov 21 '18 at 20:51













And you can also talke a close llok at your variables. What is the type of a ? what does the your append do ?

– LoneWanderer
Nov 21 '18 at 21:14





And you can also talke a close llok at your variables. What is the type of a ? what does the your append do ?

– LoneWanderer
Nov 21 '18 at 21:14












1 Answer
1






active

oldest

votes


















0














I would rather use a list comprehension like this:



def mul_mat_by_scalar(mat, alpha):
return [[alpha*j for j in i] for i in mat]


It just multiplies every element in mat by alpha and returns the new matrix.



For your approach to work, you would have to append lists and not just a. You would write something like this:



def mul_mat_by_scalar(mat, alpha):

# Write the rest of the code for question 5 below here.

mat_new =
for j in mat:
sub_mat=
for i in j:
a = i* alpha
sub_mat.append(a)
mat_new.append(sub_mat)

return mat_new


But that is ugly and not pythonic






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%2f53419947%2fmultiply-matrix-with-scalar%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














    I would rather use a list comprehension like this:



    def mul_mat_by_scalar(mat, alpha):
    return [[alpha*j for j in i] for i in mat]


    It just multiplies every element in mat by alpha and returns the new matrix.



    For your approach to work, you would have to append lists and not just a. You would write something like this:



    def mul_mat_by_scalar(mat, alpha):

    # Write the rest of the code for question 5 below here.

    mat_new =
    for j in mat:
    sub_mat=
    for i in j:
    a = i* alpha
    sub_mat.append(a)
    mat_new.append(sub_mat)

    return mat_new


    But that is ugly and not pythonic






    share|improve this answer




























      0














      I would rather use a list comprehension like this:



      def mul_mat_by_scalar(mat, alpha):
      return [[alpha*j for j in i] for i in mat]


      It just multiplies every element in mat by alpha and returns the new matrix.



      For your approach to work, you would have to append lists and not just a. You would write something like this:



      def mul_mat_by_scalar(mat, alpha):

      # Write the rest of the code for question 5 below here.

      mat_new =
      for j in mat:
      sub_mat=
      for i in j:
      a = i* alpha
      sub_mat.append(a)
      mat_new.append(sub_mat)

      return mat_new


      But that is ugly and not pythonic






      share|improve this answer


























        0












        0








        0







        I would rather use a list comprehension like this:



        def mul_mat_by_scalar(mat, alpha):
        return [[alpha*j for j in i] for i in mat]


        It just multiplies every element in mat by alpha and returns the new matrix.



        For your approach to work, you would have to append lists and not just a. You would write something like this:



        def mul_mat_by_scalar(mat, alpha):

        # Write the rest of the code for question 5 below here.

        mat_new =
        for j in mat:
        sub_mat=
        for i in j:
        a = i* alpha
        sub_mat.append(a)
        mat_new.append(sub_mat)

        return mat_new


        But that is ugly and not pythonic






        share|improve this answer













        I would rather use a list comprehension like this:



        def mul_mat_by_scalar(mat, alpha):
        return [[alpha*j for j in i] for i in mat]


        It just multiplies every element in mat by alpha and returns the new matrix.



        For your approach to work, you would have to append lists and not just a. You would write something like this:



        def mul_mat_by_scalar(mat, alpha):

        # Write the rest of the code for question 5 below here.

        mat_new =
        for j in mat:
        sub_mat=
        for i in j:
        a = i* alpha
        sub_mat.append(a)
        mat_new.append(sub_mat)

        return mat_new


        But that is ugly and not pythonic







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 21:36









        user8408080user8408080

        1,188139




        1,188139






























            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%2f53419947%2fmultiply-matrix-with-scalar%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

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen