0

Say I have a piecewise function:

V[m_] := Piecewise[{{(1 + m)^2, m >= 1}, {Log[m], m < 1}}]

And I input a large list of m values, I get three lists output: The list of V[m] values, the list of m values for which m >= 1, and the list of m values for which m < 1.

I only require an output list of the V[m] values. How do I get rid of the other two lists?

Apologies if this is an obvious question! Any help would be greatly appreciated.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257

1 Answers1

1

With the definition you have given, V isn't set up to thread over lists, which is what you seem to expect it to do. You can make that work that easy enough by giving it the Listable attribute.

SetAttributes[V, Listable]
V[m_] := Piecewise[{{(1 + m)^2, m >= 1}, {Log[m], m < 1}}]

SeedRandom[42]; data = RandomReal[4, 10]

{1.70362, 1.56409, 1.38828, 1.81496, 2.22385, 1.15668, 1.18739, 0.825631, 1.30068, 3.8933}

Now,

V[data]

gives

{7.30957, 6.57457, 5.70387, 7.92401, 10.3932, 4.65126, 4.78468, -0.191608, 5.29312, 23.9444}

It will still work as it did originally if given a single value.

V[10]

121

m_goldberg
  • 107,779
  • 16
  • 103
  • 257