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

  • Home
  • SEARCH
  • 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 7066917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:08:19+00:00 2026-05-28T05:08:19+00:00

I have some functional code that I’m trying to speed up by eliminating the

  • 0

I have some functional code that I’m trying to speed up by eliminating the for loop.

I have a set of data in x,y pairs as two vectors, so x(k) and y(k) form a pair. I also have a set of bin edges (xe). For every bin j, there is a set of x values in that bin, defined by xe(j) <= x(k) < xe(j+1). For each bin, I would like to find the mean and standard deviation of all y(k) with x(k) in that bin.

MATLAB code that accomplishes this is below:

[meany, standardeviation] = ystatsvsx (xdata, ydata, xe)

meany = zeros([size(ydata,1) (length(xe)-1)]);
standarddeviation = meany;   
[numx,bin] = histc(xdata, xe);
for j = 1:(length(xe) - 1)
   inds = bin == j;
   meany(j) = mean(ydata(inds));
   standarddeviation(j) = std(ydata(inds));
end

When xe is large, this function becomes slow. Does anyone have any suggestiosn about how to vectorize this code to eliminate the for loop? The number of data points in a given bin (numx) is variable.

One caveat: length(xe)*length(xdata) in these cases is very large (length(xdata) is always much larger than length(xe)), so it is not possible to use repmat to create a length(xe) x length(xdata) matrix.

  • 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-05-28T05:08:19+00:00Added an answer on May 28, 2026 at 5:08 am

    You can use accumarray to do that. Try something like that:

    meany = accumarray(bin(:),ydata(:),[],@mean);
    standarddeviation = accumarray(bin(:),ydata(:),[],@std);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some mutable scala code that I am trying to rewrite in a
I have some code that uses the Oracle function add_months to increment a Date
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I have some code in a couple of different functions that looks something like
I have some setup code in my functions.php file that sets permalinks and adds
I have some jQuery code. I have called an Ajax function file, file.php, that
I'd like to write a function that would have some optional code to be
In the process of changing some code, I have spilt some functions into multiple
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void

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.