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

c# - SelectList with Dictionary, add values to the Dictionary after it's assigned to SelectList -

how can i manage url using .htaccess in php? -

ios - I get the error Property '...' not found on object of type '...' -