3
$\begingroup$

im new to mathematica and trying to learn the language, but i cant get this program to work. The program is not inserting i into the list and i get the error message

Insert::ins: Cannot insert at position {1,2} in {{}}.

This happens the in the second loop because it didn't insert anything in the first. Thanks for any help in advance!! Here is the code:

myDo[v_ ] := Module[ {list = {3, 4, 5, 8, 8}, lis2 = {{}}, nr2 = 1, nr1 = 1},
    Do[
    n = list[[i]];
    If[v - n < 2, If[v - n > -2,
     Insert[lis2, i, {nr1, nr2}];
     nr2++]];
   , {i, 1, Length[list]}];]` 
$\endgroup$
5
  • $\begingroup$ Almost never use Do. Instead, use Table. $\endgroup$ Nov 14, 2018 at 18:00
  • $\begingroup$ can you post the desired results for, say, myDo[1], myDo[2], myDo[3], myD[10]? $\endgroup$
    – kglr
    Nov 14, 2018 at 18:02
  • $\begingroup$ does this give the desired results: myDo2[v_] := Module[{list = {3, 4, 5, 8, 8}, lis2 = {{}}, nr2 = 1, nr1 = 1}, Do[n = list[[i]]; If[Abs[v - n] < 2, lis2 = Insert[lis2, i, List /@ {nr1, nr2}]; nr2++], {i, 1, Length[list]}]; lis2]? $\endgroup$
    – kglr
    Nov 14, 2018 at 18:06
  • $\begingroup$ I want to find out wich numbers are close to the one i put in so for myDo[1] it would be an empyty list for myDo[2] it would have a 1 in it because the first nr(3) in the list is next to it for myDo[3] i would be 1 and 2 in a list. But this is just a small sample size for a test the size of the list would later be larger and the Distance could be larger too. $\endgroup$ Nov 14, 2018 at 18:22
  • $\begingroup$ ur solution also gives me the error message: Insert::ins: Cannot insert at position {1,2} in {{}} $\endgroup$ Nov 14, 2018 at 18:24

3 Answers 3

3
$\begingroup$
ClearAll[myDo2]
myDo2[v_] := Module[{list = {3, 4, 5, 8, 8}, lis2 = {{}}, nr2 = 1, nr1 = 1}, 
    Do[n = list[[i]];
    If[Abs[v - n] < 2, lis2 = Insert[lis2, i, {nr1, nr2++}]], {i, 1, Length[list]}]; lis2]
myDo2 /@ Range[10]

{{{}}, {{1}}, {{1, 2}}, {{1, 2, 3}}, {{2, 3}}, {{3}}, {{4, 5}}, {{4, 5}}, {{4, 5}}, {{}}}

An alternative way to generate the same output using Nearest:

ClearAll[f]
f = Module[{a = If[Head@#2 === List, List, Identity], b}, 
    b = a /. {List -> Identity, Identity -> List};
     a /@ Sort /@ Nearest[# -> "Index", b[#2], {All, 2 - $MachineEpsilon}]] & ;

lst = {3, 4, 5, 8, 8} ;
f[lst, #] & /@ {1, 2, 3}

{{{}}, {{1}}, {{1, 2}}}

f[lst, {1,2,3}]

{{{}}, {{1}}, {{1, 2}}}

f[lst, Range[10]] == myDo2 /@ Range[10] 

True

$\endgroup$
3
$\begingroup$

The statement from your code

Insert[lis2, i, {nr1, nr2}];

produces a list but leaves lis2 unchanged!

This was the source of the error message in your code (occurs on the second iteration).

You need to set lis2 equal to the output of the Insert function

lis2 = Insert[lis2, i, {nr1, nr2}];

in your code.

Further, in order to see the result, you need the expression lis2 at the end of the Module.

myDo[v_] := Module[{
   list = {3, 4, 5, 8, 8},
   lis2 = {{}},
   nr2 = 1,
   nr1 = 1
   },
  Do[n = list[[i]];
    If[v - n < 2,
     If[v - n > -2,
      lis2 = Insert[lis2, i, {nr1, nr2}];
      nr2++
      ]
     ];,
    {i, 1, Length[list]}
    ];
    lis2
  ]

myDo[7]
(* {{4, 5}} *)
$\endgroup$
2
$\begingroup$

I did the same with Table. I am not sure why double brackets are needed in the final result (and the list definitions do not need to be in the Module). I am sure the syntax can be greatly simplified, but it works. Building on the previous answer,

Setting initial elements, for v data

list = {3, 4, 5, 8, 8}; nr1 = 1; v = Range[10];

This produces a matrix where the needed results are the columns.

res = Table[lis2 = {{}}; nr2 = 1; 
If[Abs[v[[j]] - list[[i]]] < 2, 
lis2 = Insert[lis2, i, {1, nr2++}], {}], {i, 1, Length[list]}, {j,
 1, Length[v]}];

MatrixForm[res]

Output

To get the output one needs to join the elements in each column. I found two ways to do that, one with Table and one with Map, depending on the length of data v.

(*Either*)
Table[Flatten[res[[All, i]]], {i, 1, Length[v]}]
(*or*)
Flatten[res[[All, #]]] & /@ Range[Length[v]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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