Here's an answer to the question in the comments below OP:
grid={{{{0, 1, 2}, {0, 1, 3}}, {{0, 2, 2}, {0, 2, 3}}},
{{{1, 1, 2}, {1, 1, 3}}, {{1, 2, 2}, {1, 2, 3}}}}
A 2x2x2
list of 3-component vectors.
If I understood correctly, pick out, for example, all such vectors, that have 0
as their first component. I am inclined to take a pattern-matching approach.
grid /. Except[{0, _, _}, {_, _, _}] ->
Unevaluated[Sequence[]] //.
{} -> Unevaluated[Sequence[]] //.
{a_List} :> a
First line reads "take grid and replace everything that matches a 3-component vector, except a vector with 0
at the start with..."
Second line: "...a so called vanishing function. Then repeatedly apply the following rule until nothing changes..."
Third line: "...the rule is to replace empty lists with the same vanishing function. Finally repeatedly apply the rule..."
Fourth line: "...of replacing any list that is the only element of a parent list with itself".
{{{0, 1, 2}, {0, 1, 3}}, {{0, 2, 2}, {0, 2, 3}}}
Because otherwise we would get a 1x2x2
grid instead of a 2x2
grid. But if we have a different grid:
grid = {{{{0, 1, 2}, {0, 1, 3}}, {{0, 2, 2}, {0, 2, 3}}},
{{{0, 1, 2}, {1, 1, 3}}, {{1, 2, 2}, {1, 2, 3}}}}
{{{{0, 1, 2}, {0, 1, 3}}, {{0, 2, 2}, {0, 2, 3}}}, {0, 1, 2}}
Not so great. Still a 3D grid, also the last vector is at a different depth than the others. But here we can drop the last of the replacement rules:
grid /.
Except[{0, _, _}, {_, _, _}] -> Unevaluated[Sequence[]] //.
{} -> Unevaluated[Sequence[]]
{{{{0, 1, 2}, {0, 1, 3}}, {{0, 2, 2}, {0, 2, 3}}}, {{{0, 1, 2}}}}
Anyway, whatever the output, a final replacement rule to drop the 0
s:
% /. {0, a_, b_} :> {a, b}
{{{{1, 2}, {1, 3}}, {{2, 2}, {2, 3}}}, {{{1, 2}}}}
or with the original grid
and result of its processing:
{{{1, 2}, {1, 3}}, {{2, 2}, {2, 3}}}
Update
This is largely guesswork, but the way your grid is structured is suggestive.
There's a great answer somewhere here about using Flatten
with a matrix as the second argument.
This command gives all possible usage options that are applicable for your case ordered in a nice way.
MatrixForm /@ First /@ (Flatten[grid, #] & /@ Permutations[{{1}, {2}, {3}}])
The way your grid is structured,
First@Flatten[grid,{{i},{j},{k}}]
returns the four vectors with matching i
-th elements, and depending on the order of j
and k
they are arranged as {{1,2},{3,4}}
or {{1,3},{2,4}}
.
So you can also try
(First@Flatten[grid,{{i},{j},{k}}])[[All,All,{j,k}]]
e.g.
(First@Flatten[grid, {{1}, {2}, {3}}])[[All, All, {2, 3}]]
{{{1, 2}, {1, 3}}, {{2, 2}, {2, 3}}}
Part[]
in conjunction with the specificationAll
ought to be useful. BTW,out
is still a 3D array. $\endgroup$Part[grid, All, 2, All]
gives me the output I want but still with the column that I want to get rid of, and the problem I am facing is to eliminate that second column. Additionally, I am aware that the output is a 3D array, since I need sort of a "2D array for which each element is a two-coordinate vector". $\endgroup$a[i, j, k]
and you want to take a slice, eliminating the second dimension? That implies getting the arraya[i, j0, k]
wherej0
is a fixed number. What you're being given in the answers is not a 2D slice, but rather a still 3D array where ina[i, j, k]
the 3rd dimensionk
skips over the value2
. $\endgroup$grid
is a2x2x2
matrix in which each element is a 3-vector. My intention is to choose a particular value of one of the parameters (say, first parameter being0
) and choose all the values vectors, still in matrix form, that satisfy the condition. This gives me a2x2
matrix in which each element is a 3-vector with0
as the first value. Finally, I want to eliminate the first value of each vector in this2x2
matrix, since it is not relevant any more. You mean that this is not what the answer below is doing? $\endgroup$0
as their first element are restricted toa[1, j, k]
. Yes, this is what's being done. Otherwise you could end up with a case where the resulting nested list of vectors is 3D. I have an answer coming up. $\endgroup$