0

I have an nxnxn matrix which I want to loop over and replace all values < 1E-35 with 1E-35.

          for i in range(N):
              for j in range(N):
                  for k in range(N):
                      if data[i][j][k] < 1E-35:
                          data[i][j][k] = 1E-35

Doesn't seem to work.

Edit: I worked it out. It was both the indentation and incorrect indexing [i][j][k].

Problem remains: this loops from 0 to N-1 of the NxNxN data? When I do data.min() I still get values ~ 1E-101 which should be 1E-35 after the loop. Am I doing the for loop wrong?

3
  • Perhaps a few more details. In what way does it not seem to work? Also you may want to fix the indentation of your if statement. What is the datatype of data? Commented Jan 16, 2013 at 19:05
  • Griff -- If you worked it out, post it as a solution (if others haven't already) and accept it when SO allows. That way, if someone else comes along with the same problem, they'll be able to benefit from what you learned. If someone has already posted a good answer (or even a better answer then you worked out yourself), feel free to accept. Commented Jan 16, 2013 at 19:15
  • Sorry, I have amended it - it just seems so basic and that it would have been asked somewhere else if I had looked harder. Commented Jan 16, 2013 at 19:18

1 Answer 1

5

It looks to me like you are using numpy in which case, you may want np.where:

data = np.where(data < 1e-35,1e-35,data)

Alternatively, you could use fancy indexing:

data[ data < 1e-35 ] = 1e-35
Sign up to request clarification or add additional context in comments.

2 Comments

data[i,j,k] is valid in numpy?
@AshwiniChaudhary -- Yep. You can even do fun slicing: data[1:6,3:2,4:8:2]. If you ever need work with arrays as first-class objects in python, numpy is AWESOME.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.