Re: Replace list element based on a condition how to
- To: mathgroup at smc.vnet.net
- Subject: [mg106672] Re: Replace list element based on a condition how to
- From: Jan Baetens <jan.baetens at ugent.be>
- Date: Thu, 21 Jan 2010 04:49:18 -0500 (EST)
- References: <hj40od$sfg$1@smc.vnet.net>
Hi,
I think the most elegant solution would be to write a function
fun[x_] := x /. {q_ /; q - 6 < 0 -> q - 6 + 24, q_ /; q - 6 >= 0 -> q - 6}
which then can be called as fun[{18, 19, 20, 21, 22, 23, 0, 1, 2, 3}],
yielding {12, 13, 14, 15, 16, 17, 18, 19, 20, 21}.
Regards,
Jan
On 1/19/2010 11:15 AM, Canopus56 wrote:
> Q1. Is there a way to replace a list based on a condition?
>
> I have a list of integers -
>
> lst1 = {18, 19, 20, 21, 22, 23, 0, 1, 2, 3}
>
> that I would like to replace with the following rule -
>
> fncHourFix[h_] := If[(h - 6)< 0, h - 6 + 24, h - 6];
>
> but the replace function -
>
> lst2 = lst1 /. {i_} :> fncHourFix[{i}]
>
> - bombs out.
>
> I've tried several variants.
>
> Any pointers would be appreciated.
>
> Thanks - Kurt
>
>
>
>
>
--
ir. Jan Baetens
Ghent University
Department of Applied Mathematics, Biometrics and Process Control
Coupure Links 653
B-9000 Gent
Belgium
jan.baetens at UGent.be
http://users.ugent.be/~jbaetens/
tel: ++32 (0)9 264 59 31
fax: ++32 (0)9 264 62 20