Re: extracting elements from an expression
- To: mathgroup at smc.vnet.net
- Subject: [mg27683] Re: [mg27663] extracting elements from an expression
- From: BobHanlon at aol.com
- Date: Sun, 11 Mar 2001 04:04:26 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
To understand the process better, replace the values to be dropped with the
symbol d
m = {{{{1, 2}, {d, 3}}}, {{{4, 5}, {8, 9}}, {{6, 7}, {d, 10}}}, {{{11,
12}, {17, 18}}, {{13, 14}, {19, 20}}, {{15, 16}, {d, 21}}}};
Your explicit solution is
soln = Join[m[[1, 1, 1]],
Drop[m[[1, 1, 2]], 1],
m[[2, 1, 1]],
m[[2, 2, 1]],
m[[2, 1, 2]],
Drop[m[[2, 2, 2]], 1],
m[[3, 1, 1]],
m[[3, 2, 1]],
m[[3, 3, 1]],
m[[3, 1, 2]],
m[[3, 2, 2]],
Drop[m[[3, 3, 2]], 1]]
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21}
This does not just Drop items, it also rearranges them compared to Flatten
soln == DeleteCases[Flatten[m], d]
False
However, the Transpose of m is sequenced properly
soln == DeleteCases[Flatten[Transpose /@ m], d]
True
The positions to be deleted are then
Position[Transpose /@ m, d]
{{1, 2, 1, 1}, {2, 2, 2, 1}, {3, 2, 3, 1}}
a = Transpose /@ m; soln ==
Flatten[Last[Table[a = Delete[a, {k, 2, k, 1}], {k, 3}]]]
True
Or using Nest
k = 0;
soln ==
Flatten[Nest[(k++; Delete[#, {k, 2, k, 1}])&, Transpose /@ m, 3]]
True
Bob Hanlon
In a message dated 2001/3/10 1:06:08 AM, amaydeu at tinet.fut.es writes:
>I've spent several frustrating hours attempting to do something in principle
>very simple.
>After repeated failures I'm resorting to the group for help.
>
>Consider this simple expression
>
>m = {{{{1, 2}, {2, 3}}}, {{{4, 5}, {8, 9}}, {{6, 7}, {7, 10}}},
>{{{11, 12}, {17, 18}}, {{13, 14}, {19, 20}}, {{15, 16}, {16, 21}}}};
>m // TableForm
>
>I'm simply trying to obtain to obtain this list
>
>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
>21}
>
>using the positions' elements in m. This will do it the hard way
>
>Join[m[[1, 1, 1]],
> Drop[m[[1, 1, 2]], 1],
> m[[2, 1, 1]],
> m[[2, 2, 1]],
> m[[2, 1, 2]],
> Drop[m[[2, 2, 2]], 1],
> m[[3, 1, 1]],
> m[[3, 2, 1]],
> m[[3, 3, 1]],
> m[[3, 1, 2]],
> m[[3, 2, 2]],
> Drop[m[[3, 3, 2]], 1]]
>
>I've failed to do it the easy way. Here's one of my failed attemps
>
>Table[If[i == j && k == 2, Drop[m[[i, j, k]], 1], m[[i, j, k]]],
>{i, nvars}, {j, i}, {k, 2}] // Flatten
>
>which fails because I don't know how to control the k subscript
>appropriately
>as you can see by doing
>
>Flatten[Table[{i, j, k}, {i, nvars}, {j, i}, {k, 2}],2]
>