Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:08:03+00:00 2026-06-10T13:08:03+00:00

I am trying to see if there are other ways of coding this code

  • 0

I am trying to see if there are other ways of coding this code sample more efficiently. Here, y is an 1xM matrix, (say, 1×1000), and z is an NxM matrix, (say, 5×1000).

mean(ones(N,1)*y.^3 .* z,2)

This code works fine, but I worry if N increases a lot, that the ones(N,1)*y.^3 might get too wasteful and make everything slow down.

Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:08:04+00:00Added an answer on June 10, 2026 at 1:08 pm

    Its not THAT terrible for a matrix that small. Many times you can gain from the use of bsxfun on problems like this. Here the matrices are simply too small to really gain anything.

    >> N = 5;M =1000;
    >> y = rand(1,M);
    >> z = rand(N,M);
    >> mean(ones(N,1)*y.^3 .* z,2)
    ans =
          0.12412
          0.11669
          0.12102
          0.11976
          0.12196
    
    >> mean(bsxfun(@times,y.^3,z),2)
    ans =
          0.12412
          0.11669
          0.12102
          0.11976
          0.12196
    
    >> z*y.'.^3/M
    ans =
          0.12412
          0.11669
          0.12102
          0.11976
          0.12196
    

    As you can see, all three solutions return the same result. All are equally valid.

    Now I’ll compare the times required.

    >> timeit(@() mean(ones(N,1)*y.^3 .* z,2))
    ans =
       0.00023018
    
    >> timeit(@() mean(bsxfun(@times,y.^3,z),2))
    ans =
       0.00026829
    
    >> timeit(@() z*y.'.^3/M)
    ans =
       0.00016594
    

    As I said, you don’t gain much. In fact, bsxfun does not gain at all, and is even a bit slower. But you can gain a bit, if you re-write the expression into the third form I’ve posed. Not much, but a bit.

    Edit: if N is large, then the timing changes a bit.

    >> N = 2000;M = 1000;
    >> y = rand(1,M);
    >> z = rand(N,M);
    >> timeit(@() mean(ones(N,1)*y.^3 .* z,2))
    ans =
         0.034664
    
    >> timeit(@() mean(bsxfun(@times,y.^3,z),2))
    ans =
         0.012234
    
    >> timeit(@() z*y.'.^3/M)
    ans =
        0.0017674
    

    The difference is the first solution explicitly creates an expanded y.^3 matrix. This is inefficient.

    The bsxfun solution is better, because it never explicitly forms the expand y.^3 matrix. But it still forms a product matrix that is N by M. So this solution still must grab and fill a large chunk of memory.

    You should understand why the matrix-vector multiply is the best in all cases. No large matrix is ever formed. Since * is simply a dot product (thus a sum of products) it must be more efficient. Then I divide by M after the fact to create the desired mean.

    A minor improvement over the last…

    >> timeit(@() z*(y.*y.*y).'/M)
    ans =
        0.0015793
    

    which gains slightly over the power op.

    And timeit? This comes from the File Exchange, a terribly useful utility written by Steve Eddins to time code fragments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to see if there is a way in rails routing to 'return'
I am trying to see if there is an existing row in my sqlite
I'm trying to see if there is a way to build a Linq statement
I am trying to locate leaks using instruments, but the leaks I see there
NOTE: This same error also occurs when using easy_install, other ways of installation... I'm
I guess this schema of what i am trying to do will be more
I am trying to see if the user has internet connection when the app
I am trying to see if two images that the user clicked on are
I am trying to see if the controller class name is the name of
I'm trying to see if I have a correct understanding of prefetching with Core

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.