Re: 100 rows and 100 columns random matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg123458] Re: 100 rows and 100 columns random matrix
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 8 Dec 2011 05:23:10 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 12/7/11 at 6:15 AM, nazemisahar at yahoo.com (Sahar Nazemi) wrote: >I have created a 100 rows and 100 columns random matrix which its >elements are between 0 to 1(for example it consists of 0.687,...). Now >I want to know how many of these elements are in the range of "0 to >0.5" and how many are in the range of "0.5 to 1". I also use "Count" >function as below but it doesnt answer: >Count[a,{0,0.5}] ("a" is my random matrix). What this would do is count the number of times the list {0,.5} is in a. There are a couple of ways to achieve what you want. With Count I can obtain the result you want with In[16]:= a = RandomReal[1, {100, 100}]; In[17]:= Count[Flatten@a, _?(# < .5 &)] Out[17]= 4918 Here, I've used Flatten to create a 1D list and specified a pattern that matches those items that are less than .5. The result is the count of elements less than 0.5 However, for very large arrays, the pattern matching required for this approach tends to be slow. An alternative that doesn't use pattern matching that should be much faster for large arrays is: In[18]:= Total[Unitize@Chop[a, .5], 2] Out[18]= 5082 Here, I've made use of Chop to zero all elements less than 0.5, Unitize to all non-zero elements that remain to 1 and Total to add them all up. The result is the count of all elements greater than or equal to 0.5.