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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:23:56+00:00 2026-05-30T11:23:56+00:00

I am wondering whether I can do the following efficiently in Matlab. Writing a

  • 0

I am wondering whether I can do the following efficiently in Matlab. Writing a naive loop for the problem is pretty straightforward, but I am trying to find whether there are any specialized functions one could use (perhaps arrayfun / accumarray (?) — both of which I have great trouble in understanding!) Thanks in advance.

Let’s say I have two vectors as follows (in Matlab):

A = [15 4 9 6 7 5 11 3 14]; 
B = [2 7 13]; 

I would like to do the following:

  1. Sort B, if not sorted already.
  2. For every successive windows in B (i.e., [2,7], [7,13]), find the corresponding elements in A that lie within the window.
  3. In this “partitioned” A, decrement by 1 n-times from those elements in A that lie in the n-th window of B.

Example: In the above case, the first window of B is [2,7]. The elements in A that lie within this window are [5,4,3,6]. As they lie within the first window of B, I need to decrement 1, one time from each of these elements. The new A will look like the following after this operation:
A = [15 3 9 5 7 4 11 2 14];

Can this problem be reduced to a few function calls in Matlab or one should go through the naive loop business anyway? Thanks!

  • 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-30T11:23:57+00:00Added an answer on May 30, 2026 at 11:23 am

    This can be done quite easily by using the histc function to determine in what bin (what you called “window”) the values are.

    A = [15 4 9 6 7 5 11 3 14];
    B = [2 7 13];
    B = sort(B); 
    [~, bin] = histc(A, B);
    A = A - bin;
    

    edit:
    I noticed my solution is different from yours, but I suspect you made a mistake in your calculation. Do you have to subtract 2 from the values within the second bin or do you leave them as-is? If you only want to change the values in the first bin, the last line should read A(bin==1) = A(bin==1) - 1.

    A = [15 4 9 6 7 5 11 3 14]; % initial value of A
    A = [15 3 9 5 7 4 11 2 14]; % your reference result
    A = [15 3 7 5 5 4  9 2 14]; % my result (as above)
    A = [15 3 9 5 7 4 11 2 14]; % my result with A(bin==1) = A(bin==1) - 1
    

    To change in what bin a value on the edge of a bin should end up, you can try to add/subtract eps from B.

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

Sidebar

Related Questions

I was wondering whether the following can be easily implemented in OpenGL: divide window
I was wondering whether someone can shed some light on the following issue: We've
I am wondering whether I can upgrade a basic IoC container I am using
I was wondering whether or not I can extend the Enum type in C#
I am using annoted Hibernate, and I'm wondering whether the following is possible. I
I'm trying to use Java Opencl from within jruby, but am encountering a problem
Following another question on SO (I am no PHP expert), I am wondering whether
i was wondering whether i can add Aspect J to a Maven project because
i was wondering whether i can incorporate wordpress blog into a facebook application so
I am wondering whether i can add google analytic to an email. I want

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.