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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:05:53+00:00 2026-06-18T00:05:53+00:00

This is gonna be a fairly simple question to which I wonder if there

  • 0

This is gonna be a fairly simple question to which I wonder if there is a quick and clean workaround in Python.

Let say I have a nd-array defined as such:

In [10]: C = np.random.rand(2,3,3)

In [11]: C
Out[11]: 
array([[[ 0.43588471,  0.06600133,  0.81145749],
        [ 0.20270693,  0.85879686,  0.75778422],
        [ 0.68253449,  0.98287412,  0.63804605]],

       [[ 0.61591433,  0.36453861,  0.23798795],
        [ 0.26761896,  0.00657165,  0.04083067],
        [ 0.11177481,  0.55245769,  0.97274592]]])

Then I calculate the difference between a value and the previous value in the array for the 3rd dimension as follow:

In [12]: C[:, :, 1:] = C[:, :, 1:] - C[:, :, 0:C.shape[2]-1]

In [13]: C
Out[13]: 
array([[[ 0.43588471, -0.36988337,  0.74545616],
        [ 0.20270693,  0.65608994, -0.10101264],
        [ 0.68253449,  0.30033963, -0.34482807]],

       [[ 0.61591433, -0.25137572, -0.12655065],
        [ 0.26761896, -0.26104731,  0.03425902],
        [ 0.11177481,  0.44068288,  0.42028823]]])

Is it possible to come back to the original values using a similar technique or do I have to use a for loop and temporary variables?

For example, this doesn’t do the trick:

In [15]: C[:, :, 1:] = C[:, :, 0:C.shape[2]-1] + C[:, :, 1:]

In [16]: C
Out[16]: 
array([[[ 0.43588471,  0.06600133,  0.37557278],
        [ 0.20270693,  0.85879686,  0.5550773 ],
        [ 0.68253449,  0.98287412, -0.04448843]],

       [[ 0.61591433,  0.36453861, -0.37792638],
        [ 0.26761896,  0.00657165, -0.22678829],
        [ 0.11177481,  0.55245769,  0.86097111]]])
  • 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-18T00:05:54+00:00Added an answer on June 18, 2026 at 12:05 am

    First, to compute the difference, instead of

    C[:, :, 1:] - C[:, :, 0:C.shape[2]-1]
    

    you could use numpy.diff:

    np.diff(C, axis = -1)
    

    In [27]: C = np.random.rand(2,3,3)
    
    In [28]: D = C[:, :, 1:] - C[:, :, 0:C.shape[2]-1]
    
    In [29]: E = np.diff(C, axis = -1)
    
    In [30]: np.allclose(D, E)
    Out[30]: True
    

    Next, if you know you want to retrieve the original C, perhaps it is better not to overwrite the values in the first place. Just save the differences in a separate array:

    E = np.diff(C, axis = -1)
    

    After all, there is no quicker way to perform a calculation than to not compute at all :).

    But if you really do want to overwrite the values, then, to retrieve the original values, use np.cumsum:

    In [20]: C = np.random.rand(2,3,3)
    
    In [21]: D = C.copy()
    
    In [22]: C[:, :, 1:] = np.diff(C, axis = -1)
    
    In [23]: C = np.cumsum(C, axis = -1)
    
    In [24]: np.allclose(C,D)
    Out[24]: True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, this is probably gonna be a pretty simple question to answer. I haven't
Let say I have this example: class A : SomeAbstractClassWithProperties { public A(string id,
In the end this is gonna be a simple responsive slider. problems being the
This may be rather noobish but I'm gonna ask anyhow. I have a class
Ok guys I'm gonna do my best to explain this. I have a list
I have absolutely no idea how to do this, so I'm just gonna go
This should be a simple one: I have an observableArray object called To in
Maybe this is gonna sound naive and all, but is there something even remotely
Ok, this question is gonna get a lot of downvotes... I just saw this
This UITableViews gonna make me crazy! I have UITableViewCell created by - (UITableViewCell *)tableView:(UITableView

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.