Manipulating list of matrices

I have a list of around 100 matrices, that looks like this

A={{{425060., 2.14235*10^6, 0.48, 0.01, 0.39, 0.49, 0.01, 0.38,
1.64, -1.65, -2.13, 518.}, {6.65048*10^6, 934695., 0.48, 0.39,
0.43, 0.49, 0.39, 0.44, 2.2, -0.72, 0.51, 226.}, {1.24808*10^6,
1.53025*10^6, 0.04, 0.07, 0.3, 0.04, 0.07, 0.31, -0.44, -0.52,
3.36, 370.}, {4.48215*10^6, 595558., ... }.


So A[[2]], would bring in the 2nd matrix, A[[1,2]] would bring in the 2nd row of the 1st matrix and A[[1,2,2]] would bring in the 2nd element of the second row of the 1st matrix, in this case 934695.

I want to do the following:

1. Remove columns 3,4,5 and 9 from all the matrices in the list.

2. Add a column to each of the matrices whose elements are Sqrt of the sum of squares of the elements of column 6,7,8. In other words, columns 6,7,8, contains the x,y,z coordinates of a particle, and I want to calculate the distance of each particle from the origin (R). Note: I don't know how many rows are there in these matrices.

3. Each matrix contains information of a collection of particles at different Redshift Z (time), I would like to make a BubbleChart of Z vs R as shown

for the data and plot them from Redshift 20 to 30 with a step of 0.1, the size of the bubbles are decided by the first column elements.

I should note that, I have accomplished all these steps for a single matrix, however since I have around 100 of them, doing for each matrix is not the smartest way.

I lack the understanding of how to apply Map on to a list with complicated commands.

Thanks to @Gregory Rut and @RunnyKine I was able to discard the 2nd row of each matrix with element < 10^6.

• Use Select[#,#1[[2]]>10^6&]&/@matrix. You can use Map to generalize it on the whole list. Oct 10, 2014 at 7:40
• Thanks, it works! But do you have any solutions for the adding a column, after finding the no of rows? Oct 10, 2014 at 8:00
• If you want to deal with columns, just transpose your matrix using Transpose. This way the columns will turn into rows and the above method will work again. Note that you will need to transpose the matrix one more time after you finish your procedure. Oct 10, 2014 at 8:06
• This question is vague, for example, do you have the columns you want to add or are you generating it on the fly? Do you know the position of the matrix you want to add these columns. There are so many possibilities you need to be clear. For the first case of discarding rows the following should work: DeleteCases[matrixlist, x_ /; x[[2]] < 10^6, {2}], where matrixlist is your list of matrices. Oct 10, 2014 at 12:59
• Not a duplicate, but very helpful for this question. Oct 10, 2014 at 13:32

mat = {{{5.30317257, 1.64842072, 5.0780435, 5.08645518, 6.80648361,
9.43812654, 0.315671743, 7.19423081, 0.975049223,
0.947213129}, {9.56799279, 4.12088242, 0.0471024427, 8.37484587,
7.31955311, 8.74499348, 7.96797637, 4.829391, 8.39345896,
2.31362066}, {2.39440901, 1.24682889, 5.54843565, 5.40543148,
1.269972, 8.77438919, 1.16774949, 8.8872299, 6.19703019,
4.63174218}, {7.16644516, 3.74168636, 9.03911967, 7.53649879,
1.56721927, 9.60236569, 7.87332811, 9.11583843, 1.21852673,
5.24545508}}, {{0.734148305, 0.167195462, 5.68974069, 0.967768794,
1.68875857, 9.44272515, 6.06774672, 4.64820542, 7.34879725,
6.61683716}, {3.42058362, 7.99401634, 9.58063439, 8.18368391,
9.86880635, 6.90737726, 7.68507321, 1.88885883, 0.111209586,
4.57367}, {1.50033317, 4.51845628, 2.77647069, 7.96751817,
1.66934792, 4.44314175, 0.783586119, 9.2791439, 0.828392992,
2.63348224}, {1.32614248, 2.46814055, 4.5997688, 6.29204277,
6.94764656, 7.50456466, 0.586000483, 3.64872235, 0.194444861,
1.47771218}}, {{6.72809349, 7.70202709, 3.53103694, 1.18740102,
1.78920007, 8.12038293, 1.22624538, 5.2674687, 4.77198988,
1.72136709}, {3.8921759, 1.55451977, 1.0386819, 3.17480634,
3.23394573, 9.21023052, 0.031652059, 5.11123699, 0.448259866,
8.39986602}, {3.17510833, 8.14136529, 9.25473649, 0.0384855066,
8.27445843, 7.96464051, 0.116503119, 2.51780465, 2.97853468,
9.6616925}, {5.96828246, 5.1698929, 8.6097764, 7.15809872,
0.706361523, 5.82335169, 8.43562212, 9.75905176, 9.75315902,
4.31765167}}}


Suppose your list of matrices is mat. Then to remove columns 3, 4, 5 and 9 just do:

mat[[;; , ;; , {3, 4, 5, 9}]] = ## &[];

mat

{{{5.30317257, 1.64842072, 9.43812654, 0.315671743, 7.19423081,
0.947213129}, {9.56799279, 4.12088242, 8.74499348, 7.96797637,
4.829391, 2.31362066}, {2.39440901, 1.24682889, 8.77438919,
1.16774949, 8.8872299, 4.63174218}, {7.16644516, 3.74168636,
9.60236569, 7.87332811, 9.11583843, 5.24545508}}, {{0.734148305,
0.167195462, 9.44272515, 6.06774672, 4.64820542,
6.61683716}, {3.42058362, 7.99401634, 6.90737726, 7.68507321,
1.88885883, 4.57367}, {1.50033317, 4.51845628, 4.44314175,
0.783586119, 9.2791439, 2.63348224}, {1.32614248, 2.46814055,
7.50456466, 0.586000483, 3.64872235, 1.47771218}}, {{6.72809349,
7.70202709, 8.12038293, 1.22624538, 5.2674687,
1.72136709}, {3.8921759, 1.55451977, 9.21023052, 0.031652059,
5.11123699, 8.39986602}, {3.17510833, 8.14136529, 7.96464051,
0.116503119, 2.51780465, 9.6616925}, {5.96828246, 5.1698929,
5.82335169, 8.43562212, 9.75905176, 4.31765167}}}


For the second part, let's first compute the distances and store them in dist

(* Since you've deleted columns 3, 4 and 5, you now have 6, 7, and 8 in their position *)
dist = Apply[EuclideanDistance[{#3, #4, #5}, {0, 0, 0}] &, mat, {2}]

{{11.871598, 12.7783636, 12.5433807, 15.4043251}, {12.1485975,
10.5042848, 10.3178499, 8.3651098}, {9.75656308, 10.5334748,
8.35394588, 14.1530999}}


Now insert the distances for each row at the end of that row:

Transpose[Insert[Transpose[mat[[#]]], dist[[#]], -1]] & /@ Range@Length@mat

{{{5.30317257, 1.64842072, 9.43812654, 0.315671743, 7.19423081,
0.947213129, 11.871598}, {9.56799279, 4.12088242, 8.74499348,
7.96797637, 4.829391, 2.31362066, 12.7783636}, {2.39440901,
1.24682889, 8.77438919, 1.16774949, 8.8872299, 4.63174218,
12.5433807}, {7.16644516, 3.74168636, 9.60236569, 7.87332811,
9.11583843, 5.24545508, 15.4043251}}, {{0.734148305, 0.167195462,
9.44272515, 6.06774672, 4.64820542, 6.61683716,
12.1485975}, {3.42058362, 7.99401634, 6.90737726, 7.68507321,
1.88885883, 4.57367, 10.5042848}, {1.50033317, 4.51845628,
4.44314175, 0.783586119, 9.2791439, 2.63348224,
10.3178499}, {1.32614248, 2.46814055, 7.50456466, 0.586000483,
3.64872235, 1.47771218, 8.3651098}}, {{6.72809349, 7.70202709,
8.12038293, 1.22624538, 5.2674687, 1.72136709,
9.75656308}, {3.8921759, 1.55451977, 9.21023052, 0.031652059,
5.11123699, 8.39986602, 10.5334748}, {3.17510833, 8.14136529,
7.96464051, 0.116503119, 2.51780465, 9.6616925,
8.35394588}, {5.96828246, 5.1698929, 5.82335169, 8.43562212,
9.75905176, 4.31765167, 14.1530999}}}


3 is left as an exercise for the reader.

• Thanks a lot. But, could you explain the use of the {2} in "dist = Apply[EuclideanDistance[{#3, #4, #5}, {0, 0, 0}] &, mat, {2}]" , I read about the command Apply, but never came across such a line. Oct 11, 2014 at 6:41
• @NilanjanBanik, The {2} enables you to go into each row of each matrix and Apply the EuclideanDistance on that row. It's equivalent to looping without explicitly using a loop. Oct 11, 2014 at 6:47
• Thanks! wow! I couldn't find any such thing on the online Mathematica resources! Oct 11, 2014 at 6:54