0
$\begingroup$

I have a dataset with a header for row and column which consists of binaries, e.g.:

cols = ToString /@ IntegerDigits[Range[3], 2, 3]
rows = ToString /@ IntegerDigits[Range[3], 2, 3]
tbl = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

data = Dataset[AssociationThread[rows, tbl]][All, 
  AssociationThread[cols, Range[Length[cols]]]]

Now I would like to extract parts of the matrix by a pattern, e.g. row: {1,0,_} and column: {__,0,0} for the full binary this works:

data[ToString[{0, 0, 0}]][ToString[{1, 0, 0}]]

But now I would like to extract patterns like _,0,0 How can this be done? Is their a way to use a list as header for row and column instead of strings?

thanks

$\endgroup$
1
$\begingroup$

If you must use strings instead of the lists, I would say it is a bit awkward. But it is possible.

data = Dataset[AssociationThread[rows, tbl]][All, 
  AssociationThread[cols, Range[Length[cols]]]]
rowheaders = Keys@Normal@data
colheaders = Keys@Normal@data[[1]]
matchedrows = 
 Select[rowheaders, 
  StringMatchQ[#, RegularExpression["{0, 0, \\d}"]] &]
matchedcols = 
 Select[colheaders, 
  StringMatchQ[#, RegularExpression["{\\d, 0, 1}"]] &]
data[matchedrows, matchedcols]

Note that I chose selections that give a non-empty set of results, that these selections are based on regular expressions, and that we need to notice the presence of spaces in the string representations of the lists.

$\endgroup$
  • $\begingroup$ thanks @Alan. Actually I must not use strings, but I when I use lists it gives me a strange representation of the dataset: cols = IntegerDigits[Range[3], 2, 3] rows = IntegerDigits[Range[3], 2, 3] tbl = {{1, 0, 1}, {0, 1, 0}, {1, 1, 0}} data = Dataset[AssociationThread[rows, tbl]][All, AssociationThread[cols, Range[Length[cols]]]] $\endgroup$ – Wafram Jul 15 '17 at 5:59
1
$\begingroup$

You may use KeySelect and StringMatchQ.

With data as in OP with string keys.

data

Mathematica graphics

Then

data[
 KeySelect[StringMatchQ[RegularExpression["{0, 1, \\d}"]]],
 KeySelect[StringMatchQ[RegularExpression["{\\d, 1, \\d}"]]]]

Mathematica graphics

Hope this helps.

$\endgroup$

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.