Message Boards Message Boards

5 Replies
3 Total Likes
View groups...
Share this post:

count the values of an array

Posted 11 years ago
Hi everyone!

I have started using Mathematica for some weeks and I have a problem with an array. I have defined an array n of lenght m, which is an integer calculated before. Then I assign the values to n, which are integers  that stay in a range of [0,50] and then I want to count for each values from 1 to 50 how many times it occours in the array n.
I tried with the command Count[n,k] and letting variate k, but it doesen't work.
In the last experimet my array n was an array of 3 numbers



and I got

In[166]:= Count[n, 18]

Out[166]= 0

that cannot be.

Can someone help me?

Thank you so much.
5 Replies
I have to admit I don't fully understand what you asking for, but I am going to try to give a few pointers which might help you.

In Mathematica lists are entered with curly braces and elements separated by commas, like so:

If you want to make a list programmatically, you can use the Table command, like so:
Table[x^2, {x,0,10}]

This will make a list of eleven elements, like so:

If you now want a list of lists you can give another argument to Table, like so:
Table[ i*j, {i,3},{j,3}]

This will make a 3x3 matrix/array like this:
{{1, 2, 3}, {2, 4, 6}, {3, 6, 9}}

If you want to see the value of just one element you can use Part:
t =Table[i*j, {i,3},{j,3}];
Part[t,2,2] (* gives element at position row=2,column=2, which is 4 *)

A shorthand notation for Part is the double square bracket notation (not single square brackets):
t[[2,2]] === Part[t,2,2]

With all this in hand you can now use functions on rows and columns, for example:
Count[ t[[1]], 2 ]  (* count the number of 2s in the first row *)
Count[ t[[2]], 4 ]  (* count the number of 4s in the second row *)
Count[ t[[-1]], 6 ]  (* count the number 6s in the last row (-2 is the second to last row, and so on) *)
Count[ t[[All,1]], 2 ]  (* count the number of 2s in the first column (All means all rows, and 1 means first column) *)
Count[ t[[All,-2]], 6 ]  (* count the number of 6s in the next to last column *)

Now if you want to count the occurences for multiple values, like from 0 to 50, and for each row you can do:
Count[ t[[ row ]], value ],
] // Grid

And I am using Grid here to format the result in a nicer grid like format.

(The // notation is a post-fix function application, so f and x // f are equivalent)
POSTED BY: Arnoud Buzing
Posted 11 years ago
You don't specify "arrays" that way in mathematica.

You would write instead:
In[129]:= (* here you define your list of values *)
n = {1, 22, 18} ;

(* Now Count will work as expected: *)
Count[n, 18]

Out[130]= 1
POSTED BY: Markus Schopfer
I can't define a list as you suggested to me, because I don't know neither the length of the list, neither the values, as I obtain them from an expression that depends on another random variable.
For this reason I used the commandArray[n,m]
where m is the length that I have calculated from a random variable.

In particular I used the following definition

Array[n, m];
j = 1; i = 1; While[j < m + 1, While[x[] == 0, n++; i++]; i++; j++];

where x is the random list mentioned before.

Can you suggest me another way to proceed?

Thank you
Posted 11 years ago
Ok, you could do something like this:
 In[182]:= (* define your array (i.e. list) with default values 0 *)
 arr = ConstantArray[0, 50] ;
 (* set the values *)
 arr[[1]] = 1 ;
 arr[[2]] = 22 ;
 arr[[3]] = 18 ;
(* count values *)
Count[arr, 18]

Out[186]= 1

However, If the index is not important, you don't have to create an default-list of a specific size - you could simply append the Resllts to an empty list - it will grow automaticcally.
For example:
 In[187]:= (* start with an empty array *)
 arr = {} ;
 (* add the values *)
 AppendTo[arr, 1] ;
 AppendTo[arr, 22] ;
 AppendTo[arr, 18] ;
 (* count values *)
Count[arr, 18]

Out[191]= 1

Hope that helps...
POSTED BY: Markus Schopfer
Tank you so much!
Tanks to your suggestion I have solved my problem!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract