wolfram mathematica - Changing values in nested lists according to elements in the list -
I have a list of values in mathematics, for example If the second element does not reach a threshold, then how do I change the first element (3 & 5)? For example, if the second part is below 2 then I would like the first parts to go to zero. So list == Conceptually, in the normal way, in your case, code The However, note that the first method is several times faster for large numerical lists. There is one fast, but some more obscure method is: It is fast because it uses very optimized vector-operated operations which Applies to all sub-lists in Finally, if you want the final performance, then this process compiled in the C version will have another aspect of more than 2: You used it in For this last one, you need to install C compiler on your machine. List = {{3,1} , {5,4}} .
{{0,} {5,4}} . Unfortunately, some of these lists are doing manually for a very long time, unfortunately there is not an option.
will be [13]: = lst = {{3, 1}, {5, 4}} outside [13] = {{3, 1}, {5, 4}} in [14]: = thr = 2 out [14] = 2 in [15]: = map [[if [# [[2]]
# symbol here stands for argument. You can read more on pure functions, double square brackets stand for extraction. You can make it a little more condensed by using level 1, which is shortened by
@@@ :
in
[27]: = {if # [2] & lt; Thr, 0, #], # 2} & amp; @@@ lst Out [27] = {{0, 1}, {5, 4}}
in [29]: = change [{# [all, 1]] * unit stop [# [[All, 2]] - thr], # [[All, 2]]}] & amp; [Lst] out [29] = {{0, 1}, {5, 4}}
fn = compile {{{{}}, the module [ For {copy = lst, i = 1}, [i = 1, i & lt; = Length [lst], i ++, if [copy [[i, 2]] & lt; Threshold, copy [[i, 1]] = 0]]; Copy], collection-target - & gt; "C", Runtime Option - & gt; "[Speed"]
[32]: = fn [lst, 2] out [32] = {{, 1}, {5, 4}}
Comments
Post a Comment