1
$\begingroup$

another question:

I have a very long Excel data sheet of z-values. I converted the Excel sheet into CSV. to make it less heavy on calculation time.

The data is divided into packs of $197\times197$. After every pack there is a row with only zeros to divide the packs. For every block, I would like to plot a contour plot. Therefore I need to extract the individual blocks.

Something like: Split at every 197*197 Element, but skip 1*197 Element after each split (row for separation) How can I do that?

Thanks for any help!

$\endgroup$
6
  • $\begingroup$ Why not Import[], delete the zero rows, and use Partition[]? $\endgroup$ Commented Aug 2, 2016 at 16:55
  • $\begingroup$ How do you delete the zero rows ? $\endgroup$
    – henry
    Commented Aug 2, 2016 at 17:00
  • $\begingroup$ Have you seen DeleteCases[]? $\endgroup$ Commented Aug 2, 2016 at 17:02
  • $\begingroup$ Thanks. But the data is given as one long list {{-----}} so I do not want to delete all zeors. $\endgroup$
    – henry
    Commented Aug 2, 2016 at 17:08
  • $\begingroup$ You can use DeleteCases[] to delete an entire row; however, if your data has a legitimate row of zeros as opposed to just being a separator, then you should have chosen a better separator. $\endgroup$ Commented Aug 2, 2016 at 17:10

3 Answers 3

3
$\begingroup$

You could use SequenceCases, which lets you specify a replacement rule for each sequence it finds, so the delimiting zeros can be dropped that way. I'll construct a testList with a smaller number of rows and columns, then apply the splitting:

nRow = 3;
nCol = 5;
block = RandomReal[10,{nRow,nCol}];
testList = Flatten[Riffle[block, Table[{zeros}, nRow]]]

(*
==> {4.41795, 1.96463, 2.73945, 9.01363, 8.49372, 0, 0, 0, 0, 
0, 1.14841, 7.57119, 1.01441, 7.9697, 4.21982, 0, 0, 0, 0, 0, 
7.75255, 0.431971, 2.10869, 8.60605, 5.91062, 0, 0, 0, 0, 0}
*)

SequenceCases[testList, 
  {x : Except[Repeated[0, {nRow}]] .., Repeated[0, {nRow}]} :> {x}]

(*
==> {{4.41795, 1.96463, 2.73945, 9.01363, 8.49372}, 
      {1.14841, 7.57119, 1.01441, 7.9697, 4.21982}, 
      {7.75255, 0.431971, 2.10869, 8.60605, 5.91062}}
*)

In the search pattern for SequenceCases, the zeros are assumed to occur at the end of a sequence in which the preceding elements are not sequences of nRow zeros. These elements are labeled x and collected in {x} using the rule :>.

$\endgroup$
2
$\begingroup$

Using Jens' data

list =
  {4.41795, 1.96463, 2.73945, 9.01363, 8.49372, 0, 0, 0, 0, 0,
   1.14841, 7.57119, 1.01441, 7.9697, 4.21982, 0, 0, 0, 0, 0,
   7.75255, 0.431971, 2.10869, 8.60605, 5.91062, 0, 0, 0, 0, 0};

SequenceSplit (new in 11.3) seems to be custom-tailored for this task

SequenceSplit[list, {0}]

{{4.41795, 1.96463, 2.73945, 9.01363, 8.49372}, {1.14841, 7.57119, 1.01441, 7.9697, 4.21982}, {7.75255, 0.431971, 2.10869, 8.60605, 5.91062}}

With SequenceCases we can get the same result:

SequenceCases[list, {Except[0] ..}]

{{4.41795, 1.96463, 2.73945, 9.01363, 8.49372}, {1.14841, 7.57119, 1.01441, 7.9697, 4.21982}, {7.75255, 0.431971, 2.10869, 8.60605, 5.91062}}

$\endgroup$
1
$\begingroup$

Grabbing Jens' data and using Split:

list =
  {4.41795, 1.96463, 2.73945, 9.01363, 8.49372, 0, 0, 0, 0, 0,
   1.14841, 7.57119, 1.01441, 7.9697, 4.21982, 0, 0, 0, 0, 0,
   7.75255, 0.431971, 2.10869, 8.60605, 5.91062, 0, 0, 0, 0, 0};

Most /@ Cases[{_, __}]@Split[list, # =!= 0 &]

(*{{4.41795, 1.96463, 2.73945, 9.01363, 8.49372},
   {1.14841, 7.57119, 1.01441, 7.9697, 4.21982}, 
   {7.75255, 0.431971, 2.10869, 8.60605, 5.91062}}*)
$\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.