1

So let's say I have some 3x3 matrix I get with a calculation I am doing, let's say

np.array([[1,2,3],[4,5,6],[7,8,9]])

np.array([[0,0,0],[0,0,0],[0,0,0]])

I want to add this onto some matrix A and be able to access them so that if I do

> A[0]
> [[1,2,3],[4,5,6],[7,8,9]]
> A[1]
> [[0,0,0],[0,0,0],[0,0,0]]

and keep adding on bunch of these 2D array and save them with np.save('A', A) for fast access later. I kind of saw it is possible with appending to a list but I can't save a list with np.save for fast efficient access. How can I create a empty ndarray I can add matrix onto and save it all as .npy?

2 Answers 2

1

You can convert list to array it is the same:

A = list()
A.append(x)
A.append(y)
X = np.array(A)
np.save('X', X)
Sign up to request clarification or add additional context in comments.

Comments

0

Originally use this:

A = np.stack((a,b))
[[[1 2 3]
  [4 5 6]
  [7 8 9]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]]]

And once you have formed A, if you want to add another array c to A use:

A = np.vstack((A,[c]))

output for c=a:

[[[1 2 3]
  [4 5 6]
  [7 8 9]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]]

 [[1 2 3]
  [4 5 6]
  [7 8 9]]]

Comments

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.