It's not particularly efficient, but with two extension methods you can do this:
var floats = new float[3, 3] {
{ 1f, 2f, 3f },
{ 4f, 5f, 6f },
{ 7f, 8f, 9f }
};
Func<float, bool> floatToBool = f => f > 5f;
var bools =
floats
.ToJaggedArray()
.Select(array => array.Select(floatToBool).ToArray())
.ToArray()
.To2DArray();
Note that you have to manipulate it back into an "array of arrays" to get back to a 2D array.
And it would work even better if we converted to enumerable of enumerables instead of to jagged arrays. Then it could look like this:
var bools =
floats
.ToEnumerableOfEnumerables()
.Select(array => array.Select(floatToBool))
.To2DArray();
The two extension methods for the first part are something like this:
[Pure, NotNull]
public static T[][] ToJaggedArray<T>([NotNull] this T[,] p2DArray) {
var height = p2DArray.GetLength(0);
var width = p2DArray.GetLength(1);
var result = new T[height][];
for (var row = 0; row < height; row++) {
result[row] = new T[width];
for (var col = 0; col < width; col++)
result[row][col] = p2DArray[row, col];
}
return result;
}
[Pure, NotNull]
public static T[,] To2DArray<T>([NotNull] this T[][] pJaggedArray) {
if (!pJaggedArray.Any() || !pJaggedArray[0].Any())
return new T[0, 0];
var height = pJaggedArray.Length;
var width = pJaggedArray[0].Length;
var result = new T[height, width];
for (var r = 0; r < height; r++) {
var subArray = pJaggedArray[r];
if (subArray == null || subArray.Length != width)
throw new InvalidOperationException("Jagged array was not rectangular.");
for (var c = 0; c < width; c++)
result[r, c] = subArray[c];
}
return result;
}
And for the second part are something like this:
[Pure, NotNull]
public static IEnumerable<IEnumerable<T>> ToEnumerableOfEnumerables<T>(
[NotNull] this T[,] p2DArray
) {
var height = p2DArray.GetLength(0);
for (var row = 0; row < height; row++)
yield return p2DArray.GetPartialEnumerable(row);
}
[Pure, NotNull]
public static IEnumerable<T> GetPartialEnumerable<T>(
[NotNull] this T[,] p2DArray,
int pRow
) {
var width = p2DArray.GetLength(1);
for (var col = 0; col < width; col++)
yield return p2DArray[pRow, col];
}
[Pure, NotNull]
public static T[,] To2DArray<T>(
[NotNull] this IEnumerable<IEnumerable<T>> pJaggedArray
) {
if (!pJaggedArray.Any() || !pJaggedArray.First().Any())
return new T[0, 0];
var height = pJaggedArray.Count();
var width = pJaggedArray.First().Count();
var result = new T[height, width];
var r = 0;
foreach (var subArray in pJaggedArray) {
if (subArray == null || subArray.Count() != width)
throw new InvalidOperationException("Jagged array was not rectangular.");
var c = 0;
foreach (var item in subArray)
result[r, c++] = item;
r += 1;
}
return result;
}