3
$\begingroup$


Apologies if this is an already answered question, I was unable to find anything until now. I believe the answer will be quite simple, I have just been stuck on it for a few days now.
I have a large list with two layers of sublists

vecC = {{{1., 0.217963, 0.156788, 0.0559926, 0.228347, 0.240743, 0.195968, 
   0.286898, 0.00068041, 0.161386, -0.0345246, 0.185455, 0.283423, 
   0.294763, 0.384614, 0.0994098, -0.0896094, 0.261753, 0.240743, 
   0.10724, -0.054486, 0.0710168, 0.190361, 0.0163173, 0.257922, 
   0.294763, 0.161386, 0.185455, 0.25346}, {0.217963, 1., 0.0830679, 
   0.243362, 0.28446, 0.290599, 0.152392, 0.213711, -0.00825647, 
   0.0540897, 0.0751145, 0.19444, 0.189234, 0.185116, 0.367013, 
   0.0779923, 0.0125763, 0.241502, 0.290599, 0.119696, -0.118852, 
   0.410856, 0.0225762, 0.0290714, 0.130713, 0.185116, 0.0540897, 
   0.19444, 0.155425}, {0.156788, 0.0830679, 1., 0.166155, 0.144748, 
   0.109877, 0.146391, 0.143303, 0.0996916, 0.209669, -0.00988949, 
   0.214008, 0.31862, 0.0783173, 0.132977, -0.0413557, -0.0724764, 
   0.185296, 0.109877, 0.195526, -0.00800798, 0.235018, 
   0.0625874, -0.0100889, 0.129499, 0.0783173, 0.209669, 0.214008, 
   0.207037}, {0.0559926, 0.243362, 0.166155, 1., 0.220657, 0.253729, 
   0.22768, 0.0427638, -0.0129918, 0.172457, -0.017279, 0.0382562, 
   0.142621, 0.0576319, 0.121693, -0.0276115, -0.0307733, 0.101315, 
   0.253729, 0.0717787, -0.0426187, 0.226867, 0.0225817, 0.0936401, 
   0.0795733, 0.0576319, 0.172457, 0.0382562, 0.106609}, {0.228347, 
   0.28446, 0.144748, 0.220657, 1., 0.308409, 0.165781, 0.29274, 
   0.0750658, 0.225226, 0.0769847, 0.343604, 0.464982, 0.152072, 
   0.496885, 0.150435, -0.0135166, 0.354067, 0.308409, 
   0.365327, -0.190913, 0.228264, 0.298089, 0.0178117, 0.247559, 
   0.152072, 0.225226, 0.343604, 0.170803}, {0.240743, 0.290599, 
   0.109877, 0.253729, 0.308409, 1., 0.200094, 0.247223, 0.0307646, 
   0.107573, 0.0833291, 0.335692, 0.448098, 0.316107, 
   0.357461, -0.0915362, -0.0277225, 0.487421, 1., 
   0.281485, -0.213768, 0.115793, 0.280729, 0.0422386, 0.206018, 
   0.316107, 0.107573, 0.335692, 0.289007}, {0.195968, 0.152392, 
   0.146391, 0.22768, 0.165781, 0.200094, 1., 0.191784, -0.0774805, 
   0.225797, -0.0460885, 0.132348, 0.200366, 0.294147, 0.267044, 
   0.00671384, 0.0531465, 0.246438, 0.200094, 0.0822022, -0.140849, 
   0.0953459, 0.216244, 0.00796842, 0.192004, 0.294147, 0.225797, 
   0.132348, 0.23228}}, {{0.286898, 0.213711, 0.143303, 0.0427638, 
   0.29274, 0.247223, 0.191784, 1., 0.162053, 0.22473, 0.0986637, 
   0.334587, 0.321459, 0.256726, 0.328958, 0.0415536, 0.0378936, 
   0.279013, 0.247223, 0.229389, -0.089381, 0.143923, 
   0.161668, -0.0451674, 0.207513, 0.256726, 0.22473, 0.334587, 
   0.233309}, {0.00068041, -0.00825647, 0.0996916, -0.0129918, 
   0.0750658, 0.0307646, -0.0774805, 0.162053, 1., -0.0519902, 
   0.15654, 0.174082, 0.136747, -0.0339565, 0.00992398, 
   0.0278212, -0.0382499, 0.0361599, 0.0307646, 0.0606803, 
   0.0650817, -0.00242357, 0.0397274, 0.138839, 
   0.150964, -0.0339565, -0.0519902, 0.174082, 0.186734}, {0.161386, 
   0.0540897, 0.209669, 0.172457, 0.225226, 0.107573, 0.225797, 
   0.22473, -0.0519902, 1., -0.0458312, 0.140914, 0.269852, 0.180871, 
   0.226324, 0.138795, -0.100714, 0.243389, 0.107573, 
   0.189632, -0.202975, 0.209646, 0.202957, 0.0266071, 0.133996, 
   0.180871, 1., 0.140914, 0.213322}, {-0.0345246, 
   0.0751145, -0.00988949, -0.017279, 0.0769847, 
   0.0833291, -0.0460885, 0.0986637, 0.15654, -0.0458312, 1., 
   0.347256, -0.0418872, -0.101305, -0.0335616, -0.00450231, 
   0.00888515, -0.0139867, 0.0833291, 0.154562, 0.0845733, 
   0.101801, -0.0955512, 0.190919, 0.0649846, -0.101305, -0.0458312, 
   0.347256, 0.0329942}, {0.185455, 0.19444, 0.214008, 0.0382562, 
   0.343604, 0.335692, 0.132348, 0.334587, 0.174082, 0.140914, 
   0.347256, 1., 0.361735, 0.209587, 0.290634, -0.0326136, 0.0671199, 
   0.377538, 0.335692, 0.517926, -0.100502, 0.219092, 0.173128, 
   0.0673563, 0.185353, 0.209587, 0.140914, 1., 
   0.417284}}, {{0.0949247, 0.185442, 0.0330525, 0.0650521, 0.298601, 
   0.280741, 0.155844, 0.179755, 0.175704, 0.0461148, 0.271581, 
   0.351555, 0.23109, 0.162415, 0.304256, 0.00909094, 0.135341, 
   0.294497, 0.280741, 0.258659, -0.0634038, 0.142666, 0.138513, 
   0.0401322, 0.196645, 0.162415, 0.0461148, 0.351555, 
   0.17}, {0.185455, 0.19444, 0.214008, 0.0382562, 0.343604, 0.335692,
    0.132348, 0.334587, 0.174082, 0.140914, 0.347256, 1., 0.361735, 
   0.209587, 0.290634, -0.0326136, 0.0671199, 0.377538, 0.335692, 
   0.517926, -0.100502, 0.219092, 0.173128, 0.0673563, 0.185353, 
   0.209587, 0.140914, 1., 0.417284}, {1., 1., 1., 1., 1., 1., 1., 1.,
    1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
   1., 1., 1., 1., 
   1.}, {-0.054486, -0.118852, -0.00800798, -0.0426187, -0.190913, \
-0.213768, -0.140849, -0.089381, 0.0650817, -0.202975, 
   0.0845733, -0.100502, -0.223521, -0.082249, -0.243128, -0.023069, \
-0.0557377, -0.18627, -0.213768, -0.226736, 
   1., -0.103047, -0.125198, -0.00718238, -0.00909135, -0.082249, \
-0.202975, -0.100502, -0.115141}, {0.10724, 0.119696, 0.195526, 
   0.0717787, 0.365327, 0.281485, 0.0822022, 0.229389, 0.0606803, 
   0.189632, 0.154562, 0.517926, 0.500292, 0.276636, 
   0.296445, -0.0297005, 0.0308297, 0.430148, 0.281485, 1., -0.226736,
    0.328228, 0.310723, 0.0203472, 0.182388, 0.276636, 0.189632, 
   0.517926, 0.272819}, {0.240743, 0.290599, 0.109877, 0.253729, 
   0.308409, 1., 0.200094, 0.247223, 0.0307646, 0.107573, 0.0833291, 
   0.335692, 0.448098, 0.316107, 0.357461, -0.0915362, -0.0277225, 
   0.487421, 1., 0.281485, -0.213768, 0.115793, 0.280729, 0.0422386, 
   0.206018, 0.316107, 0.107573, 0.335692, 0.289007}, {0.0710168, 
   0.410856, 0.235018, 0.226867, 0.228264, 0.115793, 0.0953459, 
   0.143923, -0.00242357, 0.209646, 0.101801, 0.219092, 0.318488, 
   0.170722, 0.207891, 0.0396435, -0.0716237, 0.228591, 0.115793, 
   0.328228, -0.103047, 1., 0.208746, 0.126916, -0.0163755, 0.170722, 
   0.209646, 0.219092, 0.152321}, {0.249798, 0.178621, 0.0778838, 
   0.103019, 0.174959, 0.223817, 0.294007, 0.248723, 0.167965, 
   0.12488, 0.0333116, 0.37261, 0.281042, 0.274465, 0.2301, 
   0.120495, -0.0421227, 0.282859, 0.223817, 0.243693, -0.114316, 
   0.055469, 0.168794, 0.121367, 0.456702, 0.274465, 0.12488, 0.37261,
    0.473632}, {0.181817, 0.154268, 0.0616132, 0.081493, 0.24894, 
   0.263554, 0.259488, 0.325312, 0.0426164, 0.180956, -0.0349385, 
   0.266582, 0.331316, 0.199862, 0.416977, 0.0115553, 0.127469, 
   0.386543, 0.263554, 0.158443, -0.203263, 0.1385, 
   0.318175, -0.0333518, 0.122834, 0.199862, 0.180956, 0.266582, 
   0.161107}, {0.0163173, 0.0290714, -0.0100889, 0.0936401, 0.0178117,
    0.0422386, 0.00796842, -0.0451674, 0.138839, 0.0266071, 0.190919, 
   0.0673563, -0.068033, -0.126736, -0.0313185, 
   0.0977069, -0.103182, -0.0414052, 0.0422386, 
   0.0203472, -0.00718238, 0.126916, -0.0643211, 1., 
   0.0748332, -0.126736, 0.0266071, 0.0673563, 0.0262367}, {0.257922, 
   0.130713, 0.129499, 0.0795733, 0.247559, 0.206018, 0.192004, 
   0.207513, 0.150964, 0.133996, 0.0649846, 0.185353, 0.295123, 
   0.156421, 0.268833, 0.371724, -0.103355, 0.201009, 0.206018, 
   0.182388, -0.00909135, -0.0163755, 0.144191, 0.0748332, 1., 
   0.156421, 0.133996, 0.185353, 0.347716}, {0.294763, 0.185116, 
   0.0783173, 0.0576319, 0.152072, 0.316107, 0.294147, 
   0.256726, -0.0339565, 0.180871, -0.101305, 0.209587, 0.429202, 1., 
   0.30825, -0.0341961, -0.0187276, 0.342091, 0.316107, 
   0.276636, -0.082249, 0.170722, 0.345913, -0.126736, 0.156421, 1., 
   0.180871, 0.209587, 0.311079}, {0.161386, 0.0540897, 0.209669, 
   0.172457, 0.225226, 0.107573, 0.225797, 0.22473, -0.0519902, 
   1., -0.0458312, 0.140914, 0.269852, 0.180871, 0.226324, 
   0.138795, -0.100714, 0.243389, 0.107573, 0.189632, -0.202975, 
   0.209646, 0.202957, 0.0266071, 0.133996, 0.180871, 1., 0.140914, 
   0.213322}, {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
    1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
   1.}, {0.185455, 0.19444, 0.214008, 0.0382562, 0.343604, 0.335692, 
   0.132348, 0.334587, 0.174082, 0.140914, 0.347256, 1., 0.361735, 
   0.209587, 0.290634, -0.0326136, 0.0671199, 0.377538, 0.335692, 
   0.517926, -0.100502, 0.219092, 0.173128, 0.0673563, 0.185353, 
   0.209587, 0.140914, 1., 0.417284}}}

I also have specific positions from this list

posC = {{{1}, {2}, {3}, {4}, {5}, {6}, {7}}, {{8}, {9}, {10}, {11}, 
{12}}, {{12}, {12}, {1}, {21}, {20}, {6}, {22}, {29}, {15}, {24}, 
{25}, {14}, {10}, {1}, {12}}}

Now I'm trying to map the positions to the list. But have been unable to find an efficient way. Currently, I'm using:

alignedRes =  Function[{l}, 
   MapThread[
    vecC[[l, #1, #2]] &, {Range[1, Length@vecC[[l]]], 
     Flatten@posC[[l]]}]] /@ Range[1, Length@vecC]

(*{{1., 1., 1., 1., 1., 1., 1.}, {1., 1., 1., 1., 
  1.}, {0.351555, 1., 1, 1., 1., 1., 1., 0.473632, 0.416977, 1., 1., 
  1., 1., 1, 1.}}*)

However, I believe there must be a better way to do this. I've tried Map and MapIndexed, yet to no avail.
Thanks in advance,

$\endgroup$
2
$\begingroup$

You can use MapThread twice together with Extract:

alignedRes2 = MapThread[MapThread[Extract]@*List, {vecC, posC}];

alignedRes == alignedRes2
(* True *)

Ideally, you could use MapThread[Extract, {vecC, posC}, 2] directly, but MapThread does not support ragged lists at the moment, so we need to manually nest it. For this, we can use the operator form of MapThread for the inner one. We use Composition (@*) together with List to convert the two arguments that the outer MapThread gives us into a single list that the inner MapThread expects. Alternatively, you could write e.g.:

alignedRes2 = MapThread[MapThread[Extract, {##}] &, {vecC, posC}]
$\endgroup$
  • $\begingroup$ Thank you, that's perfect. I'm gonna look into the syntax more $\endgroup$ – Tim B Oct 23 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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