0

I'm trying to construct a 2D NumPy array from values in an extant 2D NumPy array using an iterative process. Using ordinary python lists the process I'm describing would look like so:

coords = #data from file contained in a 2D list
d = #integer
edges = []
for i in range(d+1):
    for j in range(i+1, d+1):
        edge = coords[j] - coords[i]
        edges.append(edge)

However, the NumPy array imposes restrictions that do not permit the process shown above. Below I try to do the same thing using NumPy arrays, and it should immediately be clear where the problems are:

coords = np.genfromtxt('Energies.txt', dtype=float, skip_header=1)
d = #integer
#how to initialize?
for i in range(d+1):
    for j in range(i+1, d+1):
        edge = coords[j] - coords[i]
        #how to append?

Because .append does not exist for NumPy arrays I need to rely on concatenate or stack instead. But these functions are designed to join existing arrays, and I don't have anything to concatenate or stack until after the first iteration of my loop. So I suppose I need to change my data flow, but I'm unsure how to go about this.

Any help would be greatly appreciated. Thanks in advance.

5
  • 2
    Why not stick with your first code, and then do edges_arr = np.asarray(edges)? Commented Oct 24, 2018 at 16:39
  • Because then I have to parse my coordinate file manually, instead of using numpy.genfromtxt. Moreover, I will have to do calculations involving values from both my edges array and the coords array, so I want to be consistent with my containers. Commented Oct 24, 2018 at 16:56
  • 1
    Well, we will need a minimal reproducible example with reproducible code and output that makes it clear what you're trying to do. What you have now is incomplete code and a request to fix it. Commented Oct 24, 2018 at 16:58
  • 1
    There are two reasonable ways of building arrays iteratively. 1) create the array from the list, 2) initialize an zeros array of the right size, and set 'row' values iteratively. Don't try to squeeze arrays into the list model. Commented Oct 24, 2018 at 17:01
  • 1
    coords is a (m,n) array. Looks like you want to take all the differences between rows, producing a (m,m,n) array. The only special thing is that you are trying to avoid duplicates, and get just the upper (or lower) triangle of values. Commented Oct 24, 2018 at 17:07

1 Answer 1

1

that function is numpy.meshgrid [1] , the function does it by default.

[1] https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.meshgrid.html

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, this is not what I'm looking for. That function returns a coordinate matrix from a set of coordinate vectors. I already have a matrix, representing a set of points in space that do not fit to a grid, and I want the set of non-redundant vectors that can be constructed from those points.
You search something similar to scipy.interpolate.interp2d stackoverflow.com/a/49478006/9799449

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.