0
votes

I have a matrix consisting of 3 rows and 4 columns of which which I require the central two columns.

I have attempted extracting the central two columns as follows:


a = a[[2 ;; 3, All]];

On the mathematica function list, the first entry in a[[2 ;; 3, All]] represents the rows and the second the columns, however whenever I try a[[All,2 ;; 3]] it removes the top row rather than the two columns. For some reason they seem inverted. I tried going around this by switching the entries around however, when I use a[[2 ;; 3, All]], I get the error: Part: Cannot take positions 2 through 3 in a.

I cannot wrap my head around why this keeps happening. It also refuses to extract single columns from the matrix as well.

1

1 Answers

0
votes

You show that you are assigning a variable to itself and then saying that things don't work for you. That makes me think you might have previously made assignments to variables and the results of that are lurking in the background and might be responsible for what you are seeing.

With a fresh start of Mathematica, before you do anything else, try

mat={{a,b,c,d},
     {e,f,g,h},
     {i,j,k,l}};
take23[row_]:=Take[row,{2,3}];
newmat = Map[take23, mat]

Map performs the function take23 on every row and returns a list containing all the results giving

{{b,c},
 {f,g},
 {j,k}}

If need be you can abbreviate that to

newmat = Map[Take[#,{2,3}]&, mat]

but that requires you understand # and & and it gives the same result.

If necessary you can further abbreviate that to

newmat = Take[#,{2,3}]& /@ mat

Map is widely used in Mathematica programming and can do many more things than just extract elements. Learning how to use that will increase your Mathematica skill greatly.

Or if you really need to use ;; then this

newmat = mat[[All, 2;;3]]

I interpret the documentation for that to mean you want to do something with All the rows and then within each row you want to extract from the second to the third item. That seems to work for me and instantly returns the same result.

If you instead wrote

newmat = mat[[1;;2, 2;;3]]

that would tell it that you wanted to work from row 1 down to row 2 and within those you want to work from column 2 to column 3 and that gives

{{b,c},
 {f,g}}