python - apply fromiter over matrix -


why fromiter fail if want apply function on entire matrix?

>>> aaa = np.matrix([[2],[23]]) >>> np.fromiter( [x/2 x in aaa], np.float) array([  1.,  11.]) 

this works fine, if matrix 2d, following error:

>>> aaa = np.matrix([[2,2],[1,23]]) >>> aaa matrix([[ 2,  2],     [ 1, 23]]) >>> np.fromiter( [x/2 x in aaa], np.float) traceback (most recent call last):   file "<stdin>", line 1, in <module> valueerror: setting array element sequence. 

what alternate can use? know can write 2 loops rows , columns, seems slow , not pythonic. in advance.

iterating on multidimensional matrix iterates on rows, not cells. iterate on each value, iterate on aaa.flat.

note fromiter (as documented) creates one-dimensional arrays, why have iterate on cells , not rows. if want create new matrix of other shape, you'll have reshape resulting 1d array.

also, of course, in many cases don't need iterate @ all. example, can aaa/2 divide every element of matrix 2.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -