New answers tagged

1

I think you are misunderstanding the nature of Map. It applies a head to a series of elements, then evaluates the complete expression. Please see my answer to Scan vs. Map vs. Apply for examples and further description. What you want might be handled by redefining f rather than redefining list. ClearAll[f, list, counter] f[element_] := (If[element == 1, ...


1

A solution is to manage the loop yourself, i.e. list = {0, 0, 1, 0, 0, 1, 1, 1, 1}; p = MapIndexed[Prepend[#2, #1] &, list]; f[{element_}] := (If[First[element] == 1, p = Select[p, First[#] != 1 &]]; counter++) f[{}] := counter++ counter = 0; Array[f[Cases[p, {_, #}]] &, Length[p]]; counter 9 list = Map[First, p] {0, 0, 0, 0}


1

You may use Scan and Return. Return from f after you reset the list. f[element_] := (If[element == 1, list = Select[list, # != 1 &]; Return[]]; counter++;) Then Scan the function with the Return . Scan[f, list] counter (* 4 *) Hope this helps.


5

If I understand you correctly then something like this might work for you. A = {{A11, A12}, {A21, A22}, {A31, A32}} A2 = Function[{l}, listModifier[l, #1, #2]] & @@@ A A3 = Composition @@ A2 A3[{1, 2, 3}] (* {{A11, A12}, {A21, A22}, {A31, A32}} *) (* {Function[{l}, listModifier[l, A11, A12]], Function[{l}, listModifier[l, A21, A22]], Function[{l}, ...


2

Example: LCM[37, #[[1]], #[[2]]] & /@ Transpose[{list1, list2}] or MapThread[LCM[37, ##] &, {list1, list2}] Output: {370, 1110, 1110, 2590} Reference: & # /@ etc. MapThread Transpose


2

You can use Outer along with Map. Map[LCM[37, Sequence @@ #] &, Outer[List, list1, list2], {2}] (* {{370, 370, 1110, 2590}, {555, 1110, 1110, 7770}, {185, 370, 1110, 2590}, {370, 370, 1110, 2590}} *) Hope this helps. Or as @SimonWood has pointed out in the comments. Outer[LCM[37, ##] &, list1, list2]


4

If test is a list of numbers, your code works fine: f[x_] := (If[x > 0, x*n1, 0]) test = {1, 2, 3, -1}; f /@ test (* {n1, 2 n1, 3 n1, 0} *) If you import test from an Excel file, Mathematica might give you a 2D array (Excel sheets are like matrices). You can use Flatten to convert it to a flat list, or alternatively use Part with All (please check ...



Top 50 recent answers are included