MATLAB更有效地减去矩阵

时间:2013-06-25 14:25:37

标签: matlab matrix subtraction bsxfun

我正在相互减去两个矩阵。 dataClim是30年期间每个月(12个月)的平均数据。 dataAll是1257天内的每日数据。我需要从20100101到20130611每月的每日数据中减去平均每月数据(t = 1:31是1月,t = 32-57是2月,一直到12月,然后363:393再次是1月)

这段代码有效,但我想知道是否有办法让它更有效率,更少乏味。我不知道如何写一个循环因为月份从28到31的天数不同。

% Create new array in which data_Anom is the anomaly 
% dataAnom = dataAll - dataClim 

% January
dataAnom_1 = bsxfun(@minus, dataAll(:,:,[1:31, 363:393, 728:758, 1094:1124]), dataClim(:,:,1));

% February
dataAnom_2 = bsxfun(@minus, dataAll(:,:,[32:57, 394:421, 759:787, 1125:1152]), dataClim(:,:,2));

% March
dataAnom_3 = bsxfun(@minus, dataAll(:,:,[58:88, 422:452, 788:818, 1153:1183]), dataClim(:,:,3));

% April
dataAnom_4 = bsxfun(@minus, dataAll(:,:,[89:118, 453:482, 819:848, 1184:1213]), dataClim(:,:,4));

% May
dataAnom_5 = bsxfun(@minus, dataAll(:,:,[119:148, 483:513, 849:879, 1214:1244]), dataClim(:,:,5));

% June 
dataAnom_6 = bsxfun(@minus, dataAll(:,:,[149:178, 514:543, 880:909, 1245:1255]), dataClim(:,:,6));

% July
dataAnom_7 = bsxfun(@minus, dataAll(:,:,[179:209, 544:574, 910:940]), dataClim(:,:,7));

% August
dataAnom_8 = bsxfun(@minus, dataAll(:,:,[210:240, 575:605, 941:971]), dataClim(:,:,8));

% September
dataAnom_9 = bsxfun(@minus, dataAll(:,:,[241:270, 606:635, 972:1001]), dataClim(:,:,9));

% October
dataAnom_10 = bsxfun(@minus, dataAll(:,:,[271:301, 636:666, 1002:1032]), dataClim(:,:,10));

% November
dataAnom_11 = bsxfun(@minus, dataAll(:,:,[302:331, 667:696, 1033:1062]), dataClim(:,:,11));

% December
dataAnom_12 = bsxfun(@minus, dataAll(:,:,[332:362, 697:727, 1063:1093]), dataClim(:,:,12));

% Concatenate the seperate Anomalies
dataAnom = cat(3, dataAnom_1, dataAnom_2, dataAnom_3, dataAnom_4, dataAnom_5, dataAnom_6, dataAnom_7, dataAnom_8, dataAnom_9, dataAnom_10, dataAnom_11, dataAnom_12);

clear dataAnom_*

我的一个想法是先将每个月的日子连在一起,然后每个月创建一次dataAnom。它甚至可能更慢。

% Concatenation days below to each month in dataAll into dataMon so that each month is placed together. This
% makes it easier to do the anomaly subtraction later.

dataMon = cat(3, dataAll(:,:,1:31), dataAll(:,:,363:393), dataAll(:,:,728:758) , dataAll(:,:,1094:1124),... % January
    dataAll(:,:,32:57), dataAll(:,:,394:421), dataAll(:,:,759:787), dataAll(:,:,1125:1152),... % February
    dataAll(:,:,58:88), dataAll(:,:,422:452), dataAll(:,:,788:818), dataAll(:,:,1153:1183),... % March
    dataAll(:,:,89:118), dataAll(:,:,453:482), dataAll(:,:,819:848), dataAll(:,:,1184:1213),... % April
    dataAll(:,:,119:148), dataAll(:,:,483:513), dataAll(:,:,849:879), dataAll(:,:,1214:1244),... % May
    dataAll(:,:,149:178), dataAll(:,:,514:543), dataAll(:,:,880:909), dataAll(:,:,1245:1255),... % June. Last entry goes up to 20130611, not the full month
    dataAll(:,:,179:209), dataAll(:,:,544:574), dataAll(:,:,910:940),... % July
    dataAll(:,:,210:240), dataAll(:,:,575:605), dataAll(:,:,941:971),... % August
    dataAll(:,:,241:270), dataAll(:,:,606:635), dataAll(:,:,972:1001),... % Sept
    dataAll(:,:,271:301), dataAll(:,:,636:666), dataAll(:,:,1002:1032),... % Oct
    dataAll(:,:,302:331), dataAll(:,:,667:696), dataAll(:,:,1033:1062),... % Nov
    dataAll(:,:,332:362), dataAll(:,:,697:727), dataAll(:,:,1063:1093)); % Dec

% Create dataAnom
dataAnom1 = bsxfun(@minus, dataAll(:,:,1:124), dataClim(:,:,1);
dataAnom2 = bsxfun(@minus, dataAll(:,:,125:238:), dataClim(:,:,1);
.
.
.
dataAnom12 = ...

% Combine
dataAnom = cat(3, dataAnom1, dataAnom2, dataAnom3,....);

3 个答案:

答案 0 :(得分:3)

我认为您需要重新思考如何在此处接近构建数据的方式。我会使用更好的结构化矩阵,而不是创建大量的单行数组(dataAnom1dataAll)。

最简单的是你可以使用这样的方案:31x12xNumYears会产生这样的结果:

Data = NaN(31x12xNumYears); % Blank Init
Data(1:31,1,1) = Rand(31,1); % January's populated values
Data(1:28,2,1) = Rand(28,1); % February's populated values
... % and so forth

这里的优点是矩阵操作更容易做,并且您可以更好地理解数据实际代表的内容。例如,假设dataClim是30年来的月平均值(矩阵应该是12x30)而dataAll是每日读数(矩阵应该是31x12x30),您可以执行以下操作:

subValues = NaN(31,12,30);
for yr = 1:30
    for mn = 1:12
        subValues(1:31,mn,yr) = dataAll(1:31,mn,yr) - dataClim(mn,yr);
    end
end

根据您提供给我的附加信息,我认为这是您可能正在寻找的结构类型:days x months x years x 3其中三个表示数据的lat,long和dataValue。例如:

test = rand(31,12,30,3);
lat = test(1:end,1:end,1:end,1); 
long = test(1:end,1:end,1:end,2);
data = test(1:end,1:end,1:end,3);

答案 1 :(得分:1)

你不能吗

% January
dataAnom1 = bsxfun(@minus, dataAll(:,:,[1:31 363:393 728:758 1094:1124]), dataClim(:,:,1));

?我认为这是一样的。

如果是这样,那么你可以做到

dataAnom1=zeros(size(dataAll,1),size(dataAll,2), 128*12);
for v=1:12
    dataAnom1(:,:,1+((v-1)*128:v*128)) = bsxfun(@minus, dataAll(:,:,[1:31 363:393 728:758 1094:1124]+(v-1*32)), dataClim(:,:,v));
end

(索引可能稍微偏离)

答案 2 :(得分:1)

虽然我仍然坚持你应该重构数据的事实,但你也可以使用datenum的力量来帮助你。以下内容将比您尝试手动输入日期更轻松:

clear all; clc;

% Init Data
dataAll = rand(1437,159,1258);

% Starting date of sampling.  Note that this assumes each day there was a sample, and only one sample
startDate = datenum('01-01-2010');
dateList = [0:1257] + startDate;
[yr, mn, ~, ~, ~, ~] = datevec(dateList);

% extract data depending on month that sample was taken
jan = dataAll(:,:,mn == 1);
feb = dataAll(:,:,mn == 2);
mar = dataAll(:,:,mn == 3);
... % and so forth

这应该可以获得您想要的结果。从这里,您可以进行最终的计算:

dataAnom_1 = bsxfun(@minus, jan, dataClim(:,:,1));

根据评论中的更新信息,您可以执行以下操作,根据月份和年份分隔数据:

jan2010 = dataAll(:,:,(mn == 1 & yr == 2010);
feb2010 = dataAll(:,:,(mn == 2 & yr == 2010);
... % and so forth