3
$\begingroup$

Suppose I have a list myList of ordered pairs.

  • The first element of each ordered pair represents a wavelength in nanometers.
  • The second element of each ordered pair represents an absorbance measurement in absorbance units.

I can use Select to select ordered pairs by criteria, such as ordered pairs whose wavelength is between 2 and 4 nanometers, inclusive:

(* CASE I: List, no units *) myList = {{1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}}; Select[myList, 2 <= #[[1]] <= 4 &] (* OUTPUT: *) (* {{2, 20}, {3, 30}, {4, 40}} *) 

Suppose I have a list myListQuantities of quantities (see Quantity), rather than unitless integers. Again I can use Select to select ordered pairs with wavelengths between 2 and 4 nanometers, inclusive:

(* CASE II: List, including units *) myListQuantities = { {Quantity[1, "Nanometers"], Quantity[10, "AbsorbanceUnits"]}, {Quantity[2, "Nanometers"], Quantity[20, "AbsorbanceUnits"]}, {Quantity[3, "Nanometers"], Quantity[30, "AbsorbanceUnits"]}, {Quantity[4, "Nanometers"], Quantity[40, "AbsorbanceUnits"]}, {Quantity[5, "Nanometers"], Quantity[50, "AbsorbanceUnits"]} }; Select[myListQuantities, Quantity[2, "Nanometers"] <= #[[1]] <= Quantity[4, "Nanometers"] &] (* OUTPUT: *) (* { {Quantity[2, "Nanometers"], Quantity[20, "AbsorbanceUnits"]}, {Quantity[3, "Nanometers"], Quantity[30, "AbsorbanceUnits"]}, {Quantity[4, "Nanometers"], Quantity[40, "AbsorbanceUnits"]} } *) 

But what if I now store the same data in a QuantityArray? For example:

(* CASE III: QuantityArray, including units *) myQuantityArrayUnits = QuantityArray[ {{1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}}, {"Nanometers", "AbsorbanceUnits"}]; 

The same sort of selection by criteria with Select doesn't work:

(* CASE III: QuantityArray, including units (attempt 1) *) Select[myQuantityArrayUnits, 2 <= #[[1]] <= 4 &] (* OUTPUT: *) (* Select: Nonatomic expression expected at position 1 in Select... *) (* ---------- *) (* CASE III: QuantityArray, including units (attempt 2) *) Select[myQuantityArrayUnits, Quantity[2, "Nanometers"] <= #[[1]] <= Quantity[4, "Nanometers"] &] (* OUTPUT: *) (* Select: Nonatomic expression expected at position 1 in Select... *) 

How can I make selections — ideally using Select — from a QuantityArray?

(I could convert the QuantityArray to a list of lists of quantities before making the selection, but I'd prefer not to have to do that.)

$\endgroup$
1
  • 2
    $\begingroup$ Select[myQuantityArrayUnits // Normal, 2 <= QuantityMagnitude[#[[1]]] <= 4 &] // QuantityArray $\endgroup$ Commented Jun 15 at 19:08

1 Answer 1

5
$\begingroup$

I think you're going to need to do a transformation, since QuantityArray is a kind of an opaque data structure. For example:

Select[QuantityMagnitude[myQuantityArrayUnits], 2 <= #[[1]] <= 4 &] Select[QuantityMagnitude[myQuantityArrayUnits], Between[{2, 4}]@*First] Select[Normal[myQuantityArrayUnits], Quantity[2, "Nanometers"] <= #[[1]] <= Quantity[4, "Nanometers"] &] Select[Normal[myQuantityArrayUnits], Between[{Quantity[2, "Nanometers"], Quantity[4, "Nanometers"]}]@*First] 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.