4
$\begingroup$

I have a file (attached as an image) which I imported to Mathematica and only extracted the first and second column. My problem is that I want to extract the data beginning from the row where $A=0$. Assuming that the extraction of the 2 columns is called $s$ I could easily use Drop Drop[s, 13], but I want the same for more files in which the 0 is not always at the beginning of the 14th row. I think I should wrap it in a function of the empty entries, but I have no clue how to do it.

Does anyone have suggestions for what my approach should be?

Revamped image description

$\endgroup$
0

5 Answers 5

9
$\begingroup$

Your data will be stored as a list of lists, so I'm just approximating that by using random numbers and letting A be list element number one. Then I would do this:

data = RandomInteger[10, {20, 3}]
Drop[data, LengthWhile[data, #[[1]] != 0 &]]

The new list will always start with a line where A is zero, so for example:

{{0, 8, 10}, {1, 7, 9}, {5, 1, 5}, {2, 10, 0}, {8, 0, 6}, {3, 1,
3}, {1, 5, 0}, {1, 10, 9}, {4, 2, 8}, {8, 4, 1}, {4, 10, 10}, {0, 6,
2}, {8, 0, 0}, {8, 4, 10}, {1, 6, 0}, {6, 8, 1}, {9, 1, 9}}

$\endgroup$
3
$\begingroup$

The edited title rather contains a hint to an alternative: Position

SeedRandom[1];
data = RandomInteger[10, {20, 3}]~Prepend~{"x", "y", "z"};
Drop[data, Position[data, {0, __}, {1}, 1][[1, 1]] - 1]

(* {{0, 2, 6}, {4, 5, 4}, {3, 0, 1}, {3, 5, 3},
    {0, 3, 2}, {3, 9, 5}, {1, 5, 2}, {3, 9, 1},
    {0, 4, 4}, {1, 5, 2}, {7, 9, 9}, {8, 10, 0}, {10, 10, 7}} *)

It's pretty fast, but that's not likely to be an issue, unless the first zero occurs rather far down the spreadsheet.

$\endgroup$
2
$\begingroup$

Using SequenceReplace:

SequenceReplace[list, {{Except[0], __} .., k : {_, _, _}} :> k, 1]

{{0, 2, 6}, {4, 5, 4}, {3, 0, 1}, {3, 5, 3}, {0, 3, 2}, {3, 9, 5}, {1, 5, 2}, {3, 9, 1}, {0, 4, 4}, {1, 5, 2}, {7, 9, 9}, {8, 10, 0}, {10,
10, 7}}

$\endgroup$
2
$\begingroup$
SeedRandom[1];
data = RandomInteger[10, {20, 3}];

Using SequenceSplit:

patt = {a_, b__} /; a == 0;

SequenceSplit[data, {s : patt} :> s][[2 ;;]] /. m_?MatrixQ :> Splice@m

(*{{0, 2, 6}, {4, 5, 4}, {3, 0, 1}, {3, 5, 3}, {0, 3, 2}, {3, 9, 5}, {1,5, 2},   
   {3, 9, 1}, {0, 4, 4}, {1, 5, 2}, {7, 9, 9}, {8, 10, 0}, {10, 10, 7}}*)

Or using SplitBy:

SplitBy[data, #[[1]] != 0 &][[2 ;;]] /. m_?MatrixQ :> Splice@m

(*{{0, 2, 6}, {4, 5, 4}, {3, 0, 1}, {3, 5, 3}, {0, 3, 2}, {3, 9, 5}, {1,5, 2},   
   {3, 9, 1}, {0, 4, 4}, {1, 5, 2}, {7, 9, 9}, {8, 10, 0}, {10, 10, 7}}*)

I add a very good version that @Syed has provided me:

SequenceSplit[data, k : {{0, _, _} ..} :> k] // Rest // Catenate

SplitBy[data, #[[1]] != 0 &] // Rest // Catenate
$\endgroup$
2
  • 1
    $\begingroup$ SequenceSplit[data, k : {{0, _, _} ..} :> k] // Rest // Catenate and SplitBy[data, #[[1]] != 0 &] // Rest // Catenate $\endgroup$
    – Syed
    Jan 19 at 6:07
  • $\begingroup$ Thanks, Syed! I've added the versions you just suggested to my answer. :-) $\endgroup$ Jan 19 at 6:15
1
$\begingroup$
SeedRandom[1];
list = RandomInteger[10, {20, 3}];

Using DeleteElements (new in 13.1) and TakeWhile

DeleteElements[list, TakeWhile[list, #[[1]] != 0 &]]

Using MapAt and LengthWhile

MapAt[Nothing, ;; LengthWhile[list, #[[1]] != 0 &]] @ list

Using Part and FirstPosition

list[[First @ FirstPosition[list, {0, __}] ;;]]

Using Part and Split

list[[Length @ First @ Split[list, #[[1]] != 0 &] ;;]]

All produce

{{0, 2, 6}, {4, 5, 4}, {3, 0, 1}, {3, 5, 3}, {0, 3, 2}, {3, 9, 5}, {1, 5, 2}, {3, 9, 1}, {0, 4, 4}, {1, 5, 2}, {7, 9, 9}, {8, 10, 0}, {10, 10, 7}}

$\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.