2
$\begingroup$

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]}
$\endgroup$
  • $\begingroup$ something like list = {1, 2, 3, Null, 4}; MapAt[#^2 &, list, Position[list, x_ /; ! (x === Null), {1}, Heads -> False]] ? $\endgroup$ – Harry Feb 16 '15 at 11:08
  • $\begingroup$ or list = {1, 2, 3, Null, 4}; #^2 & /@ Select[list, ! (# === Null) &] ? $\endgroup$ – Harry Feb 16 '15 at 11:11
  • $\begingroup$ Using the Listable attribute it usually much faster than using Map in this way, so you could try F^2 /. Null^2 -> Null. $\endgroup$ – C. E. Feb 16 '15 at 11:12
  • $\begingroup$ @Harry right, something like that. But I would also like to control the resulting value, is it possible? $\endgroup$ – SuTron Feb 16 '15 at 11:17
  • 1
    $\begingroup$ Related: mathematica.stackexchange.com/questions/9784/… $\endgroup$ – Michael E2 Feb 16 '15 at 11:30
3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – ThunderBiggi Oct 3 '16 at 11:23
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Oct 3 '16 at 23:34
  • $\begingroup$ 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 $\endgroup$ – ThunderBiggi Oct 4 '16 at 13:20
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Oct 5 '16 at 0:31
3
$\begingroup$

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})

$\endgroup$
1
$\begingroup$
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]]]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.