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 6128737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:39:39+00:00 2026-05-23T16:39:39+00:00

I have a function which does the following loop many, many times: for cluster=1:max(bins),

  • 0

I have a function which does the following loop many, many times:

for cluster=1:max(bins), % bins is a list in the same format as kmeans() IDX output
    select=bins==cluster; % find group of values
    means(select,:)=repmat_fast_spec(meanOneIn(x(select,:)),sum(select),1); 
    % (*, above) for each point, write the mean of all points in x that 
    % share its label in bins to the equivalent row of means
    delta_x(select,:)=x(select,:)-(means(select,:)); 
    %subtract out the mean from each point
end

Noting that repmat_fast_spec and meanOneIn are stripped-down versions of repmat() and mean(), respectively, I’m wondering if there’s a way to do the assignment in the line labeled (*) that avoids repmat entirely.

Any other thoughts on how to squeeze performance out of this thing would also be welcome.

  • 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-23T16:39:39+00:00Added an answer on May 23, 2026 at 4:39 pm

    Here is a possible improvement to avoid REPMAT:

    x = rand(20,4);
    bins = randi(3,[20 1]);
    
    d = zeros(size(x));
    for i=1:max(bins)
        idx = (bins==i);
        d(idx,:) = bsxfun(@minus, x(idx,:), mean(x(idx,:)));
    end
    

    Another possibility:

    x = rand(20,4);
    bins = randi(3,[20 1]);
    
    m = zeros(max(bins),size(x,2));
    for i=1:max(bins)
        m(i,:) = mean( x(bins==i,:) );
    end
    dd = x - m(bins,:);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JavaScript code: Link In which the function makewindows does not
I have a function which does the following: When the function is called and
We have a JavaScript function named move which does just windows.location.href = any given
I have a helper function, which basically calls CompareTo on two objects, but does
I have a class named toto which I send to a function that does
I have the following JavaScript code which is inside a function which I am
Let's say there is a function x() which does some animation If I loop
I have the following script which does not work 100%, it returns about 20
I have the following situation: I have a certain function that runs a loop
I have the following (crude) function, which continually watches a directory for new files

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.