Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7012

Re: 100 rows and 100 columns random matrix

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: 100 rows and 100 columns random matrix
Date 2011-12-08 10:27 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jbq3e4$ijr$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 12/7/11 at 6:15 AM, nazemisahar@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.

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: 100 rows and 100 columns random matrix Bill Rowe <readnews@sbcglobal.net> - 2011-12-08 10:27 +0000

csiph-web