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


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

Re: 100 rows and 100 columns random matrix

From "Nasser M. Abbasi" <nma@12000.org>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: 100 rows and 100 columns random matrix
Date 2011-12-08 10:32 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jbq3n1$ion$1@smc.vnet.net> (permalink)
References <jbniam$497$1@smc.vnet.net>

Show all headers | View raw


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









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


Thread

100 rows and 100 columns random matrix Sahar Nazemi <nazemisahar@yahoo.com> - 2011-12-07 11:23 +0000
  Re: 100 rows and 100 columns random matrix "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-12-08 10:23 +0000
  Re: 100 rows and 100 columns random matrix "Nasser M. Abbasi" <nma@12000.org> - 2011-12-08 10:32 +0000
  Re: 100 rows and 100 columns random matrix David Skulsky <edskulsky@gmail.com> - 2011-12-08 10:36 +0000

csiph-web