number of element in array/matrix

T

Tommy Grav

Hi,
I have what I suspect to be a fairly simple problem while using
python Numeric.
I am attempting to count the number of times that an element 'b'
occurs in
numeric array 'a'. I tried unsuccessfully to find a more efficient
function to
do this for me such as that offered when using a list, but couldn't
seem to find
anything for Numeric arrays. However, I suspect that my loop is not
the most
efficient way to achieve this.

def countel(a, b): #counts the number of times value 'b' is found
in array 'a'
i=0
count=0
while (i<len(a)):
j=0
while (j<len(a[0])):
if (a[j]==b):
count=count+1
else:
pass
j=j+1
i=i+1
return count

Any help or advice would be greatly appreciated,
Matt


something like this?
>>> import numpy as n
>>> a = n.matrix([[1,2,3,4,1],[2,3,4,1,2],[3,4,1,2,3]],"float")
>>> a
matrix([[ 1., 2., 3., 4., 1.],
[ 2., 3., 4., 1., 2.],
[ 3., 4., 1., 2., 3.]])matrix([[1, 0, 0, 0, 1],
[0, 0, 0, 1, 0],
[0, 0, 1, 0, 0]])
Cheers
Tommy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top