3

Considering I have a list:

F = {f1, f2, f3, f4}

I want to perform exponent on the list

F = Power[#, 2]& /@ F

Now I want to do the same but with conditions. I want to perform Power[#, 2] on the elements of F that are not equal to NULL and the result of Power[#, 2] is not null.

Thanks.

UPDATE:

Let's say I have

`A = {x, y, z}`

SomeAction[t_] := ...

SomeAction[#] &/@ A

Let's say x = NULL

As Harry commented SomeAction[#] &/@ Select[A, !(# === NULL) &] will work only on y and z.

Let's also say that SomeAction[y] = NULL, and in this case I want to take the original y.

So I want to have following A = ...SomeAction[#] &/@ Select[A, !(# === NULL) &]... be equivalent to

A = {y, SomeAction[z]}
SuTron
  • 1,708
  • 1
  • 11
  • 21
  • something like list = {1, 2, 3, Null, 4}; MapAt[#^2 &, list, Position[list, x_ /; ! (x === Null), {1}, Heads -> False]] ? – Harry Feb 16 '15 at 11:08
  • or list = {1, 2, 3, Null, 4}; #^2 & /@ Select[list, ! (# === Null) &] ? – Harry Feb 16 '15 at 11:11
  • Using the Listable attribute it usually much faster than using Map in this way, so you could try F^2 /. Null^2 -> Null. – C. E. Feb 16 '15 at 11:12
  • @Harry right, something like that. But I would also like to control the resulting value, is it possible? – SuTron Feb 16 '15 at 11:17
  • 1
    Related: http://mathematica.stackexchange.com/questions/9784/map-a-function-across-a-list-conditionally – Michael E2 Feb 16 '15 at 11:30

3 Answers3

4

For example, here is a way to do what you want with one single function.

In the following example, instead of Null, the "forbiden" value is 1, and someactionis computing Mod[x, 2] function (which gives the remainder on division of x by 2).

Let's define someaction, Mathematica allows to do this :

someaction[x_List] := someaction /@ DeleteCases[x, 1];

someaction[x_] := Mod[x, 2] // Switch[#, 1, x, _, #] &
(* or also someaction[x_] := Mod[x, 2] /. {1 -> x}; *)

The first definition tells what someaction should do when its argument is a List: it deletes all 1's from the list and map on this new list the more general definition of someaction.
The second (more general) defintion of someaction tells the function to perform a particular computation (here Mod[x,2]) but if 1is the result, it will be converted back to the argument (x).

Test

Let's say my test list is :

mylist={1,2,3,4,5,6};

then

someaction[mylist]

returns as expected

{0, 3, 0, 5, 0} 

(for comparison : Mod[#,2]/@mylist returns {1, 0, 1, 0, 1, 0})

SquareOne
  • 7,575
  • 1
  • 15
  • 34
4

Here is a construct you might like:

ifNot[f_, x_][y_] /; MatchQ[y, x] := y

ifNot[f_, x_][y_] := If[MatchQ[#, x], y, #] & @ f[y]

Example:

ifNot[#^2 &, 4] /@ {1, 2, 3, 4, 5}
{1, 2, 9, 4, 25}

This is designed to work with patterns as well as literals for parameter x.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Shouldn't the fourth number in the answer to the example be 16? At least this is what I calculated in my head and what my Mathematica (11) gives me – ThunderBiggi Oct 03 '16 at 11:23
  • @user1482714 I think you misunderstand the purpose of this function, which is: if the input is not "4" then square it, else return it unmodified. – Mr.Wizard Oct 03 '16 at 23:34
  • Ohh, sorry, I was doing it with 2 for the x_ argument, as this is what I needed in my application. You are completely right and my comment is wrong – ThunderBiggi Oct 04 '16 at 13:20
  • @user1482714 No problem. We all make mistakes; I seem to make more than my share of them. Please continue to call me out when you believe you have spotted one of my mistakes. – Mr.Wizard Oct 05 '16 at 00:31
1
Clear[myList];
myList = {{1, 3.128}, {2, 2.459}, {2, 6.287}, {7, 6.02}, {2, 15287}};

myListLength = Length[myList];
For[i = 1, i <= myListLength, i++,
 If[myList[[i, 1]] == 2, Print[myList[[i]]]]]
Artem
  • 11
  • 2