0
$\begingroup$

Given a list; I have the following program. It will check the list of numbers in the first column. If some numbers are missing by giving a step size. It will add the missing number with a "Nulls" in the second and third column respectively.

Nullloop[data_, start_, stop_, step_] :=
   Module[{i, j, collect = {}, m, n, o}, finnal = {};
       For[{i = start, j = 1},
           i <= stop,
           { m = data[[j, {1, 2, 3}]],
             n = data[[j, 1]], 
             o = {i, Null, Null}, 
             If[n ==  i, 
               collect = Append[collect, m], 
               collect = Append[collect, o]
               ],
             i = i + step,
             j = j + 1, 
             Clear[n, o, m]
           }]; 
           finnal = collect];

Then I run the following program

data1 = {{0, 1, 2}, {0.1, 4, 5}, {0.4, 3, 1}, 
         {0.6, 10, 2}, {0.7, 4,10}};
Nullloop[data1, 0.0, 0.7, 0.1]

But it gives the following

{{0.0,1,2},{0.1,4,5},{0.2,Null,Null},{0.3,Null,Null}}

Then it stopped with some error,

Part::partw: Part 6 of {{0,1,2},{0.1,4,5},{0.4,3,1},{0.6,10,2},{0.7,4,10}} does not exist. >>

what I want is the following

{{0.0,1,2},{0.1,4,5},{0.2,Null,Null},

{0.3,Null,Null},{0.4, 3, 1},{0.5,Null,Null}, 

{0.6, 10, 2}, {0.7, 4,10}}
$\endgroup$
  • $\begingroup$ It stopped because you are testing i <= stop in the loop and since you called Nullloop[data1, 0.0, 0.3, 0.1] then stop == 0.3...? $\endgroup$ – Marius Ladegård Meyer Oct 12 '16 at 7:33
  • $\begingroup$ Also note that For loops and Append/AppendTo are typically discouraged in Mathematica. Try to use Map, Table, or similar, or if you have to loop explicitly, use Do and Reap/Sow to collect results. $\endgroup$ – Marius Ladegård Meyer Oct 12 '16 at 7:35
  • $\begingroup$ See alternatives to loops... $\endgroup$ – ubpdqn Oct 12 '16 at 7:57
  • $\begingroup$ Hi sorry for that, I mean even if you put Nullloop[data1,0.0,0.7,0.1]. It gives the error. I have edited my question. Sorry that I did not notice that it was 0.3 while I was writing this question. It is my fault and I apologize. $\endgroup$ – TM90 Oct 12 '16 at 8:22
  • $\begingroup$ instead of just saying "some error" you should include the actual error (see edit). Clearly there is a flaw in your logic causing j to go beyond the length of data. (indeed moving the j=j+1 inside the If fixes it, but as said For loops are never a good way to go) $\endgroup$ – george2079 Oct 12 '16 at 13:55
2
$\begingroup$

Here are a couple of ways to achieve that result.

Function[{i}, 
  If[Length@# == 1, #[[1]], {i, Null, Null}] &@ 
   Select[data1, #[[1]] == i &]] /@ Range[0, .7, .1]

Sort@Join[data1, {#, Null, Null} & /@ 
     Complement[Range[0, .7, .1],data1[[All, 1]], 
        SameTest -> (Chop[#1 - #2] == 0 &)]]

{{0, 1, 2}, {0.1, 4, 5}, {0.2, Null, Null}, {0.3, Null, Null}, {0.4, 3, 1}, {0.5, Null, Null}, {0.6, 10, 2}, {0.7, 4, 10}}

note the second case illustrates another issue: you can not count on equality of floating point numbers.

$\endgroup$
  • $\begingroup$ Hi george2079, I have one more question. Could you please give a brief introduction of this special symbols "#","@#","@","&@" etc... $\endgroup$ – TM90 Oct 13 '16 at 1:33
  • $\begingroup$ The link does not exist $\endgroup$ – TM90 Oct 13 '16 at 2:42
  • $\begingroup$ reference.wolfram.com/language/guide/Syntax.html $\endgroup$ – george2079 Oct 13 '16 at 2:43

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.