4
$\begingroup$

I have the following text: loadeddata={{1,2,3},{5,2,1},{1,4,1},{1,5,0},{2,3,5}}

How can I removed the third numbers in each of the sublists?

The real data has 600 of these sublists. I want to use Histogram3D with the first two variables.

$\endgroup$
  • 5
    $\begingroup$ loadeddata[[All, {1, 2}]] or loadeddata[[All, 1 ;; 2]] $\endgroup$ – Bob Hanlon Sep 26 '16 at 2:47
  • 2
    $\begingroup$ I think people are too quick to vote close on newbies. $\endgroup$ – M.R. Sep 26 '16 at 3:41
  • 1
    $\begingroup$ If you like to use a pattern then loadeddata /. {a_, b_, c_} -> {a, b} $\endgroup$ – bobbym Sep 26 '16 at 5:32
4
$\begingroup$

Here's a convenience function for you:

DropColumn[mat_, n_] := Module[
    {toDrop, toKeep, numberOfColumns, normalized, fixNegs},
    numberOfColumns = Length[mat[[1]]];
    fixNegs = Function[If[Less[#, 0], numberOfColumns + # + 1, #]];
    toDrop = If[ListQ[n], Map[fixNegs, n], {fixNegs @ n}];
    toKeep = Complement[Range @ numberOfColumns, toDrop];
    Return[mat[[All, toKeep]]]
   ];

Here's how you use it:

loadeddata = {{1, 2, 3}, {5, 2, 1}, {1, 4, 1}, {1, 5, 0}, {2, 3, 5}};
loadeddata = DropColumn[loadeddata, 3]

enter image description here

$\endgroup$
  • $\begingroup$ Can you explain what advantage it has over loadeddata[[All, 1 ;; 2]]? $\endgroup$ – Kuba Sep 26 '16 at 9:22
  • $\begingroup$ Let's you specify the columns you want to delete as opposed to the columns you don't? $\endgroup$ – M.R. Sep 26 '16 at 14:31
  • $\begingroup$ I see, makes sense. If you disagree with closing you can vote to reopen. $\endgroup$ – Kuba Sep 26 '16 at 14:45

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