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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:35:08+00:00 2026-05-23T14:35:08+00:00

Say I have an array of values: values = 1:100; an array of indices:

  • 0

Say I have an array of values:

values = 1:100;

an array of indices:

interval_indices = [40 45 80];

and an array of quantities that I would like to add to the elements in values:

quantities_to_add = [5 -9 30];

I am looking for a compact expression in MATLAB (maybe using accumarray?) that allows me to add the elmements of quantities_to_add to the elements in values depending on the indices specified by indices.

If I were to do this manually:

values(1:interval_indices(1))                         = values(1:interval_indices(1)) + ...
                                                         quantities_to_add(1);
values(interval_indices(1):interval_indices(2))       = values(interval_indices(1):interval_indices(2)) + ... 
                                                         quantities_to_add(2);
% and so forth

values(interval_indices(end-1):interval_indices(end)) = values(interval_indices(end-1):interval_indices(end)) + ...
                                                         quantities_to_add(end);
  • 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-23T14:35:08+00:00Added an answer on May 23, 2026 at 2:35 pm

    EDIT:

    Actually, this is a much smarter and vectorized way of doing the same:

    lastIndx=interval_indices(end);   
    quantitiesVector=zeros(1,lastIndx);
    quantitiesVector([1,interval_indices(1:end-1)+1])=[quantities_to_add(1) diff(quantities_to_add)];
    
    newValues=[values(1:lastIndx)+cumsum(quantitiesVector),values(lastIndx+1:end)];
    

    Previous answer:

    One way of doing it is using arrayfun to generate a vector of quantities and then adding.

    intervalLength=diff([0 indices]);
    lastIndx=interval_indices(end);
    quantities=cell2mat(arrayfun(@(x)ones(1,intervalLength(x))*quantities_to_add(x),1:numel(indices),...
        'UniformOutput',false));
    
    newValues=[values(1:lastIndx)+quantities, values(lastIndx+1:end)];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array collection that I would like to limit to say 100
Lets say I have an array numbers that contains the following values: int numbers
I have a 2D bitmap-like array of let's say 500*500 values. I'm trying to
Let's say I have an array of 100 random integers values. Instead of storing
Say I have an $input array, that contains something like this : array 0
Say I have an array of values: string[] text = new string[] { val1,
Let's say I have an array of lots of values (C++ syntax, sorry): vector<double>
Say you have 100000000 32-bit floating point values in an array, and each of
Say I have an array that represents a set of points: x = [2,
let say I have an array of this values: $arr = array(190, 2215, 2,

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.