Re: elementwise vector scalar subtraction
- To: mathgroup at smc.vnet.net
- Subject: [mg35640] Re: elementwise vector scalar subtraction
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 24 Jul 2002 02:06:00 -0400 (EDT)
- References: <ahj04h$goi$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Oscar, Some suggestions: t = {0, 1, 1.5, 2, 2.5, 4}; c = {a, b}; TC[t_, c_] := Module[{u}, u = Transpose[ReleaseHold[t + Hold[c]]]; u[[All,1]] = t[[1]]; u ] Test: TC[t, c] {{0, 1 + a, 1.5 + a, 2 + a, 2.5 + a, 4 + a}, {0, 1 + b, 1.5 + b, 2 + b, 2.5 + b, 4 + b}} To include assigning values to tca, tcb we may use TC1[t_, c_] := Module[{u}, Clear["`tc*"]; u = Transpose[ReleaseHold[t + Hold[c]]]; u[[All,1]] = t[[1]]; Evaluate[ToExpression[(StringJoin["tc", ToString[#1]] & ) /@ c]] = u ]; (*the use of Evaluate is because in evaluating an expression h[e,...]=rhs or h[e,...]:= expr the evaluation of h[e,...] stops after evaluating the heads and the elements (at h*[e*,...]) --- otherwise we could not re-define. It could have been avoided by introducing another symbol, v say, with v = ToExpression[ ...]; v=u*) Test: TC1[t, c] {{0, 1 + a, 1.5 + a, 2 + a, 2.5 + a, 4 + a}, {0, 1 + b, 1.5 + b, 2 + b, 2.5 + b, 4 + b}} tca {0, 1 + a, 1.5 + a, 2 + a, 2.5 + a, 4 + a} It may be more convenient to use indexed symbols: TC2[t_, c_] := Module[{u}, u = Transpose[ReleaseHold[t + Hold[c]]]; u[[All,1]] = t[[1]]; Evaluate[tc /@ c] = u ]; TC2[t, c] {{0, 1 + a, 1.5 + a, 2 + a, 2.5 + a, 4 + a}, {0, 1 + b, 1.5 + b, 2 + b, 2.5 + b, 4 + b}} tc[a] {0, 1 + a, 1.5 + a, 2 + a, 2.5 + a, 4 + a} -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "O.A. Linares, M.D., Ph.D." <divinesaam at aol.com> wrote in message news:ahj04h$goi$1 at smc.vnet.net... > Dear Colleagues, > > I have a t-vector > > t = {0, 1, 1.5, 2, 2.5, 4, 6, 8, 10, 15, 20, 30}; > > I want to subtract 0.15 from each element except the first to get > > tc15 = {0, 1 - 0.15, 1.50 - 0.15, 2 - 0.15, 2.5 - 0.15, 4 - 0.15, 6 - > 0.15, 8 - 0.15, 10 - 0.15, 15 - 0.15, 20 - 0.15, 30 - 0.15}; > > I then want to subtract 0.20 to get > > tc20 = {0, 1 - 0.20, 1.50 - 0.20, 2 - 0.20, 2.5 - 0.20, 4 - 0.20, 6 - > 0.20, 8 - 0.20, 10 - 0.20, 15 - 0.20, 20 - 0.20, 30 - 0.20}; > > Is there a way to automate this process for n subtractions from 0.15 > to 0.30 to yield individual t-vectors for tc15 through tc30 ? > > With Much Appreciation, > > Oscar >