How to use "movsum()" with panel data in a table using loop mechanisms?
Afficher commentaires plus anciens
I have the following 606 x 281 table ('ABC'):

- - -
Goal: For each date, create 2 new variables (e.g. 'LSum' and 'USum'). 'LSum' should calculate the sum of all cell values across the columns (4-281), but only with those values whose header is in the cell array of ABC.L, for that specific date. In the same fashion, 'USum' should calculate the sum of all cell values across the columns, but only with those values whose header is in the cell array of ABC.U, for that specific date.
- - -
How I would start:
% load content
load ('ABC.mat');
% run through every date, starting from the top
for row=1:size(ABC,1);
% for-loop for 'L' that determines for what specific cells (of col. 4-281) the following calculation has to be done: how?
% for-loop for 'U' that determines for what specific cells (of col. 4-281) the following calculation has to be done: how?
% now generate new variables
LSum = sum(); % But how can I use if clause here to select only eligible cells that enter into the sum calculation?
USum = sum(); % Same problem here as LSum
end;
% Concatenate table ABC and the newly formed variables into 1 table
ABC = [ABC(:,1:3) LSum USum ABC(:,3+1:end)];
- - -
Thanks in advance for your help, especially for the looping through date and the cell arrays of 'L' and 'U' at the same time.
Réponse acceptée
Plus de réponses (1)
Vishal Neelagiri
le 3 Jan 2017
You can use Properties.VariableNames to access the header names of the table ABC. Then you can use these property names to compare them with the 1 X 27 cell elements of either L or U. You can refer to the code below:
props = ABC.Properties.VariableNames;
% props should be a 1*281 cell array consisting of the header names of the columns
LSum = 0;
for k=1:606;
for i=1:27
for j=4:281
if (strcmp(props{j},ABC.L{1,i})
LSum = LSum + ABC{k,j};
end
end
end
end
You can use a similar workflow to calculate USum as well.
2 commentaires
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!