MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: 100 rows and 100 columns random matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123467] Re: 100 rows and 100 columns random matrix
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Thu, 8 Dec 2011 05:24:54 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jbniam$497$1@smc.vnet.net>
  • Reply-to: nma at 12000.org

On 12/7/2011 5:23 AM, Sahar Nazemi wrote:
> Hello
> I'm a student of geology who works with
> Mathematica sofware .I'm learning it by myself recently.
> I have a question and will appreciate if you
> help me in this regard.
> 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).
> Regards
> Sahar Nazemi

The important thing to note is that some functions take
a 'pattern' to look for in a list (such as Cases, Position),
and some functions take a 'criteria' to look for in a list
(such as Select).

This is the first thing you need to decide on.

So, what you are looking for above is a criteria (#<0.5), and
not a pattern. Hence you can use Select

----------------
n  = 5; (*in your case this will be 100 *)
(a = Table[RandomReal[], {n}, {n}]) // MatrixForm
p  = Select[Flatten[a], # < 0.5 &];
m  = Length[p]
------------------------

If you want to use Cases, you can, but need to do a little
more work in order to make it a pattern so that Cases is happy

--------------------------------------
p = Cases[Flatten[a], x_ /; x < 0.5]
m = Length[p]
------------------------------------

Notice in all the above, Flatten was used to make the 'matrix'
into a 'vector'.

If you want to find the positions of these elements in the matrix,
then do

------------------------------
Position[a, x_ /; x < 0.5]
-----------------------------

If you want to replace those elements which are <0.5 in the matrix,
by another value, say Null, then do

-----------------------------------
p = Position[a, x_ /; x < 0.5];
a = ReplacePart[a, p -> Null]
-----------------------------------

To find how many are >=0.5, just take the difference

--------------------------------
n  = 5; (*in your case this will be 100 *)
(a = Table[RandomReal[], {n}, {n}]) // MatrixForm
p  = Select[Flatten[a], # < 0.5 &];
m  = Length[p];

n^2-m
-------------------------

--Nasser












  • Prev by Date: Re: Preventing unwanted threading by Inner
  • Next by Date: Re: Transparent textures from images
  • Previous by thread: Re: 100 rows and 100 columns random matrix
  • Next by thread: Preventing unwanted threading by Inner