3

I have a problem which I hope contributors can help me solve. I think it is best just to provide a working example:

I have two cells both of which consist of the same number of matrices (the result of reading a series of data files followed by some loop calculations). Each matrix is a column of decimal year days followed by a series of columns of data. Here is the dummy data:

A = [ 186.356 1 2 3 4;186.364 2 3 4 5;186.372 3 4 5 6]
B = [ 187.356 1 2 3 4;187.364 2 3 4 5;187.372 3 4 5 6]
C = [ 188.356 1 2 3 4;188.364 2 3 4 5;188.372 3 4 5 6]
x = {A,B,C}
D = [ 186.3568 1 2 3 4; 186.3576 2 3 4 5; 186.3584 3 4 5 6; 186.3592 4 5 6 7; 186.36 5 6 7 8; 186.3608 6 7 8 9; 186.3616 7 8 9 10; 186.3624 8 9 10 11; 186.3632 9 10 11 12; 186.364 10 11 12 13; 186.3648 11 12 13 14; 186.3656 12 13 14 15]
E = [ 187.3568 1 2 3 4; 187.3576 2 3 4 5; 187.3584 3 4 5 6; 187.3592 4 5 6 7; 187.36 5 6 7 8; 187.3608 6 7 8 9; 187.3616 7 8 9 10; 187.3624 8 9 10 11; 187.3632 9 10 11 12; 187.364 10 11 12 13; 187.3648 11 12 13 14; 187.3656 12 13 14 15]
F = [ 188.3568 1 2 3 4; 188.3576 2 3 4 5; 188.3584 3 4 5 6; 188.3592 4 5 6 7; 188.36 5 6 7 8; 188.3608 6 7 8 9; 188.3616 7 8 9 10; 188.3624 8 9 10 11; 188.3632 9 10 11 12; 188.364 10 11 12 13; 188.3648 11 12 13 14; 188.3656 12 13 14 15]
y = {D,E,F}

My intention is to sum the data columns contained within both x and y. However you can see that the resolution of the data in y is much higher than x therefore I would first like to average the data in y based upon the timesteps of x.

As an example the first time period which matches between x and y correspond to row 1 in matrix A but only the first 10 rows in matrix D. The sum of the first row in A is 10:

sumA = sum(A(1,2:end),2)

and the average of the first 10 rows in D is

sumD = sum(mean(D(1:10,2:end)),2)

resulting in a total of 38.

This is a simple example; I have many rows of data in two large cells. I suspect I need to extract the data from the cells, loop through the data whilst rewriting to another cell of the same dimensions as the first two cells, x and y but am at a loss as to where to start. Any help would be great.

Edit

In looking to clarify my problem I realise I made a mistake in the original question. This is no doubt the cause of the confusion.

Everything above is correct however the sum of the first 10 rows of D:

sumD = sum(mean(D(1:10,2:end)),2)
sumD =

28

should actually be added to the sum of the second row in A:

sumA = sum(A(2,2:end),2)
sumA =

14

This is because all the values in rows 1-10 of column 1 in matrix D are larger than the the value in row 1 and column 1 of matrix A but smaller than or equal to row 2 and column 2 of matrix A. It might be easier if increase the dummy data in matrix D:

D = [ 186.3568 1 2 3 4; 186.3576 2 3 4 5; 186.3584 3 4 5 6; 186.3592 4 5 6 7; 186.36 5 6 7 8; 186.3608 6 7 8 9; 186.3616 7 8 9 10; 186.3624 8 9 10 11; 186.3632 9 10 11 12; 186.364 10 11 12 13; 186.3648 11 12 13 14; 186.3656 12 13 14 15; 186.3664 13 14 15 16; 186.3672 14 15 16 17; 186.368 15 16 17 18; 186.3688 16 17 18 19; 186.3696 17 18 19 20; 186.3704 18 19 20 21; 186.3712 19 20 21 22; 186.372 20 21 22 23]

Now the result would be a two value vector. The first value would be 28+14, the result of the sum of the second row in A (or sumA) and the sum of the mean of the first 10 rows of data in matrix D (or sumD). The second value would the sum of the third row in A, lets say sumA2:

sumA2 = sum(A(3,2:end),2)
sumA2 =

18

and sumD2:

sumD2 = sum(mean(D(11:end,2:end)),2)
sumD2 =

68
sumA2+sumD2

ans =

86

I would like this process to be automated so that I can go through each matrix in the cell. i.e. if I start with cells x and y with dims:

x = 

[300x5 double]    [300x5 double]    [300x5 double]
y = 

[2000x5 double]    [2000x5 double]    [2000x5 double]

I would like the result to be

z = 

[300x1 double]    [300x1 double]    [300x1 double]

I am not sure if that makes things any clearer but lets see!

4

1 回答 1

2

好吧,如果我设法正确地获得了您所有棘手的规范,那么代码如下:

function z = foo(x, y)
  z = x;
  for i = 1:length(x)
    z{i} = sum(z{i}(:, 2:end), 2);  
    dmin = 0;
    for j = 1:size(x{i}, 1)
      dmax = x{i}(j, 1);
      t = y{i}(:, 1);
      mask = t > dmin & t <= dmax;
      if any(mask)
        z{i}(j) = z{i}(j) + sum(median(y{i}(mask, 2:end)), 2);
      end
      dmin = dmax;
    end
  end
end

对于给定x的问题,对于我y的答案,和zz{1} == z{2} == z{3}

>> z{1}
ans =
10
42
70

D如果我从您的“编辑”部分替换,我会得到z{1}(3) == 86,正如您所声称的。

代码没什么特别的。dmin并根据来自(即,等)dmax的矩阵第一列的值保持当前日期范围。需要一个语句来避免从空数组中获取中位数,这会导致's 的向量,这会破坏总和。xABif any(mask)NaN

于 2013-07-16T14:46:39.183 回答