【发布时间】:2016-12-31 08:50:19
【问题描述】:
我有下表('ABC'):
目标:为每个日期创建 2 个新变量(例如“LSum”和“USum”)。 'LSum' 应该计算列 Universe (4-281) 中所有单元格值的总和,但仅限于那些标题在 ABC.L 的单元格数组中的值,用于该特定日期。以同样的方式,'USum' 应该计算列中所有单元格值的总和,但仅限于那些标题在 ABC.U 的单元格数组中的值,对于该特定日期。
% 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)];
感谢您的帮助,尤其是同时循环遍历日期和 'L' 和 'U' 的元胞数组。
【问题讨论】:
-
请向我们展示您已经尝试过的内容,并解释您遇到的任何困难。
-
%load content:
load ('ABC.mat');%run through each date, 从顶部开始:for row=1:size(ABC,1);%for-loop for 'L' 确定哪些特定单元格(第 4-281 列) 必须进行以下计算:如何? 'U' 的 %for-loop 确定 col 中的特定对象。 4-281 必须进行以下计算:如何? %现在生成新变量:LSum = sum();%但是如何使用 if 子句只选择符合条件的单元格?USum = sum(); end; end; end;%将表 ABC 和新形成的变量连接到一张表中:ABC = [ABC(:,1:3) LSum USum ABC(:,3+1:end)]; -
请编辑问题并在其中添加此代码。
-
完成!请参阅上面的 Dev-iL 部分。谢谢
标签: matlab loops if-statement for-loop