3
$\begingroup$

I want to drop a specified number of elements from a 'table

tab = {{0.2421321014434619`, 0.27498187797759344`, 
0.28728191976974427`, 0.29287047412596257`, 
0.3494318810308082`}, {0.3359182983747898`, 0.3202252266080714`, 
0.3061767427469966`, 0.2940938477605253`, 0.283709859405582`, 
0.2748580785260618`, 0.26738319813875666`, 0.260872981665339`, 
0.2552516206579761`, 0.24782249478634893`, 0.24506299142920446`, 
0.2515128419306529`, 0.2763667585306461`}, {0.3295856393559455`, 
0.4054474246779005`, 0.4728093810396434`, 0.5134063809878686`, 
0.5333138745142322`, 0.5430480032279276`, 0.5487423826396918`, 
0.5529829411464551`, 0.5566173670733299`, 0.5597417807658861`, 
0.5622517273279026`, 0.5633464838462574`, 0.5616964104105145`, 
0.5554888928831214`, 0.5427398185856183`}, {0.5221263952096429`, 
0.4942177548200779`, 0.46217124328270465`, 0.4307733594356792`, 
0.4042335523051115`, 0.38462253251886747`, 0.3719341799201244`, 
0.365039801860099`, 0.36255066810131026`, 0.36325840876256693`, 
0.3662575405512424`, 0.37092114290185174`, 0.37683431863786276`, 
0.38372997094566247`, 0.3914392747519866`, 0.39985697190794944`, 
0.4089182021630236`, 0.41858330402921934`, 0.42882815708967575`, 
0.4396378543249029`, 0.4510026370899692`}};

I tried to use for loop

For[j = 1, j <= Length[tab], j++,
 Drop[tab[[j]], -2]
 ]

but it doesn't seem to work.

Please note that this is only an example - in my case the number of dropped elements is also a function of index j and not constant as in the example above.

What can I do?

$\endgroup$
1
  • 2
    $\begingroup$ Your function is not saving the result of Drop, that's the reason it's not working. $\endgroup$
    – vapor
    Apr 27, 2016 at 17:04

3 Answers 3

4
$\begingroup$

The first thing you should do is to forget about using For-loops to manipulate lists. If you feel more comfortable with For-loop type thinking then you might use

 Table[Drop[tab[[i]], -2], {i, Length[data]}]

But if you are willing to learn a bit functional coding, I'd recommend

Drop[#, -2] & /@ tab
$\endgroup$
1
  • $\begingroup$ Since your answer matches with the code in my head almost completely, I shall mark it as accepted. Btw: I would LOVE to learn functional coding. And links where to start? Sometimes it's also hard to understand your answers without understanding the details. $\endgroup$
    – skrat
    Apr 27, 2016 at 17:20
6
$\begingroup$

If you need to get involved with a function of index j, you might find MapIndexed helpful. For example, if you want to drop j th element in the jth sublist, you can do

MapIndexed[Drop[#1, #2] &, tab]

Here #2 is your {j}, and you can feed it to your function. Be aware of the difference of {j} and j in Drop.

$\endgroup$
4
$\begingroup$

If we are to drop the first 2 elements of tab[[1]], the last 2 of tab[[2]], the first 6 of tab[[3]] and the last 2 of tab[[4]] make use of MapThread, e.g.

MapThread[Drop, {tab, {2, -2, 6, -2}}]
{{0.287282, 0.29287, 0.349432},
 {0.335918, 0.320225, 0.306177, 0.294094, 0.28371, 0.274858, 
  0.267383, 0.260873, 0.255252, 0.247822, 0.245063},
 {0.548742, 0.552983, 0.556617, 0.559742, 0.562252, 
  0.563346, 0.561696, 0.555489, 0.54274}, 
 {0.522126, 0.494218, 0.462171, 0.430773, 0.404234, 0.384623, 
  0.371934, 0.36504, 0.362551, 0.363258, 0.366258, 0.370921, 0.376834, 
  0.38373, 0.391439, 0.399857, 0.408918, 0.418583, 0.428828}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.