0

I am trying to to grow an array/matrix with each iteration within a for loop. Following is my code

import numpy as np


sino = []; 
for n in range(0, 4):
    fileName = 'D:\zDeflectionBP\data\headData_zdef\COSWeighted_trunk_' + str(n) + '.bin'
    f = open(fileName, "rb")
    data = np.fromfile(f, np.float32)
    sino = np.append(sino, data)
f.close()

fileName = 'D:\zDeflectionBP\data\headData_zdef\Head_FFS_COSWeighted.bin'
f = open(fileName, "wb")
f.write(bytes(sino))
f.close()

Each iteration the data is loaded witThere four

However, in the end, I found the size (in terms of number of bytes) of sino is twice as it should be.

For example: Each size of data: 3MB then, since I have four data, the size of the sino should be: 3MB X 4 = 12MB. But I found the size of the size is 24MB.

What is happening here? I'd like for sino to be only 12MB, which only contains data from the four data variable. How should I do it? Thanks.

4
  • Please, stick with list append! Hardly anyone uses np.append correctly. If you must use np.concatenate. That way you are forced to pay close attention to the dimensions of the arrays that you join. Commented Feb 22, 2018 at 20:03
  • @hpaulj: I am sorry I don't quite understand. Should I use or not use 'np.append()', or should I use 'np.concatenate()' instead? Commented Feb 22, 2018 at 20:11
  • Other recent questions where np.append gives problems: stackoverflow.com/questions/48913179/…, stackoverflow.com/questions/48900858/…, stackoverflow.com/questions/48847798/… Commented Feb 22, 2018 at 20:11
  • @hpaulj: you mean I should use np.concatenate() instead? Commented Feb 22, 2018 at 20:13

1 Answer 1

2

Your sino isn't a numpy array initially but a Python list.

Numpy converts it to a 64 bit array the first time by default on a 64 bit installation, after that it stays that way, twice as large as you expected.

All the times you append data it's converted to 64 bit, since that's the format of the target.

Make sino a np.float32 array right from the start to solve the problem.

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

3 Comments

I see. How do I decalre it to be a numpy float32 array?
Example: numpy.array([], dtype="float32"). And see remark of @hpaulj.
Got it. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.