Message Boards Message Boards

1
|
5524 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:
GROUPS:

count the values of an array

Posted 12 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
n[1]=1

n[2]=22

n[3]=18

and I got

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

Out[166]= 0

that cannot be.

Can someone help me?

Thank you so much.
5 Replies
Tank you so much!
Tanks to your suggestion I have solved my problem!

Bye
Posted 12 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
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:
{1,2,3,4}

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:
{0,1,4,9,16,25,36,49,64,81,100}

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:
Table[
Count[ t[[ row ]], value ],
{row,3},
{value,0,50}
] // 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)

http://reference.wolfram.com/mathematica/ref/List.html
http://reference.wolfram.com/mathematica/ref/Table.html
http://reference.wolfram.com/mathematica/ref/Part.html
http://reference.wolfram.com/mathematica/ref/Count.html
http://reference.wolfram.com/mathematica/ref/
Grid.html
http://reference.wolfram.com/mathematica/ref/Postfix.html
POSTED BY: Arnoud Buzing
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 12 years ago
Hi,
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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract