2
$\begingroup$

I have this Table which I am trying to transform by removing all the elements that are smaller than -5 and larger than 5 from the rows but not including the first element in each row (not sure that makes sense).

test = Table[Flatten[{i, Table[j, {j, -10, 10, 1}]}], {i, 1, 10}] 

I would like to end up with what is equivalent to this:

Table[Flatten[{i, Table[j, {j, -5, 5, 1}]}], {i, 1, 10}] 

Obviously the table is not that simple and it is not generated like this. It is imported from a file and I am trying to prune.

I tried this:

test[[All, 2 ;; Length[test[[1]]]]] = Table[Select[test[[i, 2 ;; Length[test[[1]]]]], -5 <= # <= 5 &], {i, 1, Length[test]}] 

But it gives the wrong result. I am sure I am on the right path I just can't see it!

Edit: here is a link to the actual data file.

Then thing is when I apply the solution proposed by @Syed I end up with an uneven Table. The rows do not have the same length anymore. I am trying to figure out what to do

The solution is good for the problem I asked! Thank you, the problem is my data is weirder than I thought.

$\endgroup$

3 Answers 3

3
$\begingroup$
test = Table[Flatten[{i, Table[j, {j, -10, 10, 1}]}], {i, 1, 10}]; Join[{First@#}, Select[Rest@#, -5 <= # <= 5 &]] & /@ test (* or *) Prepend[Select[Rest@#, -5 <= # <= 5 &], First@#] & /@ test 

{{1, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {2, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {3, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {4, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {7, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {8, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {9, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {10, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}} 
$\endgroup$
4
$\begingroup$

UnitStep[x - a] numerically codes $1 \mapsto x >= a$ and $0 \mapsto x < a$.
UnitStep[b - x] numerically codes $1 \mapsto x <= b$ and $0 \mapsto x > b$.
Their product codes $1 \mapsto a <= x <= b$ and $0 \mapsto x < a \vee x > b$.

crit = UnitStep[test + 5] UnitStep[5 - test]; crit[[All, 1]] = 1; Pick[test, crit, 1] 

It's efficient on large datasets. On small ones, not so much.

test = Table[ Flatten[{i, RandomInteger[{-10, 10}, 100]}], {i, 1, 1000}]; a1 = Join[{First@#}, Select[Rest@#, -5 <= # <= 5 &]] & /@ test; // AbsoluteTiming a2 = Prepend[Select[Rest@#, -5 <= # <= 5 &], First@#] & /@ test; // AbsoluteTiming a3 = test // Module[{crit}, crit = UnitStep[# + 5] UnitStep[5 - #]; crit[[All, 1]] = 1; Pick[#, crit, 1] ] &; // AbsoluteTiming (* {0.03099, Null} {0.026923, Null} {0.002548, Null} *) a1 == a2 == a3 (* True *) 

Ref.:

$\endgroup$
0
$\begingroup$
test = Table[Flatten[{i, Table[j, {j, -10, 10, 1}]}], {i, 1, 10}]; nw = NestWhile[Take[#, 2 ;; -2] &, #, Length[#] > 12 &] & /@ (Rest /@ test); MapIndexed[Prepend[#1, #2[[1]]] &, nw] 

yields:

{{1, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {2, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {3, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {4, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {5, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {7, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {8, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {9, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}, {10, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}} 
$\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.