I think you just want to use sum here, and not movsum, since you're summing over different columns each time. Regardless, this is fairly straightforward with table indexing, actually. The strategy would be:
- Iterate over the rows
- Compute the L_sum value for that row by L_sum(i) = sum(T{i, T.L(i,:)});
- Repeat the same for the U_sum.
- Assign the results into the table.
Here's a small example on just the L_sum part:
>> L = [ { 'ABC', 'GHI' }; { 'DEF', 'GHI' }; { 'ABC', 'DEF' } ];
>> ABC = randn(3,1);
>> DEF = randn(3,1);
>> GHI = randn(3,1);
>> L_sum = zeros(height(T),1);
for i = 1:height(T)
L_sum(i) = sum(T{i, T.L(i,:)});
end
>> T.L_sum = L_sum
T =
3×5 table
L ABC DEF GHI L_sum
______________ _______ _________ ________ _______
'ABC' 'GHI' -1.3499 -0.063055 -0.12414 -1.474
'DEF' 'GHI' 3.0349 0.71474 1.4897 2.2044
'ABC' 'DEF' 0.7254 -0.20497 1.409 0.52044
There are probably fancier ways of doing this, but I think this might be the easiest to look at and understand.
Best Answer