Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: setting elements of an array based on a condition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87169] Re: setting elements of an array based on a condition
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 3 Apr 2008 05:14:06 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <fsvejr$ssp$1@smc.vnet.net>

Claus wrote:

> I have one array, W, which for testing purposes has 25 rows and 50 columns.
> There are two more things I want to accomplish with this:
> 1) I want to set all elements of W which are smaller than zero to zero.

Among many other ways to do it,

     W = Clip[W, {0, Infinity}]

> 2) Then I want to calculate the sum of the elements in each column of W.
> How can I do this?

     Total@W

<snip>

Here is a running example of the above.

X = RandomReal[{0, 1}, 50];
Y = RandomReal[{0, 1}, 50];

BinWidth = 0.2;
Radius = 0.2;

xyzVals2 =
   Table[{i/(1/BinWidth) + (BinWidth/2),
     j/(1/BinWidth) + (BinWidth/2)}, {i, 0, (1/BinWidth) - 1}, {j,
     0, (1/BinWidth) - 1}];

RasterPtsVals = Partition[Flatten[xyzVals2], 2]

RasterX = RasterPtsVals[[All, 1]]
RasterY = RasterPtsVals[[All, 2]]

XDiffSq = (RasterX - a /. a -> X)^2;
YDiffSq = (RasterY - b /. b -> Y)^2;
Dist = Sqrt[(XDiffSq + YDiffSq)];

W = 1 - (Dist/Radius)

W = Clip[W, {0, Infinity}] (* Negative values are set to zero *)

Total@W (* Sum by columns *)

[... output discarded ...]

Here is W

Out[12]= {{-3.44977, -3.42712, -1.38462, -1.26534, -1.08044, \
-2.98646, -2.22725, -3.6861, -3.40791, -3.46006, -3.28652, -3.41354, \
-2.8053, -2.86024,
   0.906464, -3.20979, -0.716005, -2.46973, -2.80043, -2.05446, \
[... output discarded ...]
-2.15641, -1.84735, -2.72521, -1.6088, -4.45553, -1.20688,
   0.55019, -4.8174, -0.927821,
   0.529696, -2.5054, -1.2456, -0.522142, -0.823514, -2.39116, \
-0.780842}}

This is W after clipping

Out[13]= {{0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0.906464, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.47876, 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.576626, 0., 0., 0.57248,
   0., 0., 0., 0., 0., 0., 0., 0.}, {0., 0., 0., 0., 0., 0., 0., 0.,
[... output discarded ...]
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.55019, 0., 0., 0.529696,
   0., 0., 0., 0., 0., 0.}}

Sum by columns

Out[14]= {0.501202, 1.12529, 1.0363, 0.869559, 1.04313, 1.06559, \
1.04856, 0.997975, 1.02411, 1.00233, 0.882582, 0.584701, 1.03934, \
0.999631, 0.906464, 0.939218, 0.67531, 0.973813, 0.999577, 1.05409, \
0.863479, 0.925528, 0.999161, 0.875093, 1.01133, 0.990823, 0.893494, \
1.03689, 0.949295, 1.06598, 0.999567, 0.907048, 0.770375, 1.00052, \
1.01669, 1.06436, 1.1272, 0.989273, 0.976439, 0.526029, 0.915948, \
0.665679, 0.983972, 1.08602, 1.15502, 1.03827, 0.789926, 0.990356, \
1.05427, 0.976073}


Regards,
-- Jean-Marc



  • Prev by Date: Re: Mathematica notebooks the best method for technical
  • Next by Date: Re: Rotation of 3D objects
  • Previous by thread: Re: setting elements of an array based on a condition
  • Next by thread: Re: Defining a Play[] function for Drawbar Organ Emulation