开发者

what the mean of the following MATLAB code

please help me to understand this code:

x(:,i) = mean( (y(:,((i-1)*j+1):i*j)), 2 )';

i can't find it in my boo开发者_StackOverflow社区k. thanks.


The code you posted can be made more readable using temporary variables:

        a       = (i-1)*j+1;
        b       = i*j;
        val     = y(:,a:b);
        x(:,i)  = mean( val, 2 )'; %# =mean( val' )

What exactly you do not understand? For meaning of mean , : and ' consult matlab help.


It would help if you said exactly what you don't understand, but here are a few tips:

  1. if you have something like a(r,c), that means matrix a, row r, column c (always in this order). In other words, you should have two elements inside the brackets separated by a comma where the first represents the row, the second the column.

  2. If you have : by itself in one of the sides of the comma, that means "all". Thus, if you had a(r,:), then you would have matrix a, row r, all columns.

  3. If : is not alone in one of the sides of the comma, then it will mean "to". So if you have a(r, z:y), that means matrix a, row r, columns z to y.

  4. Mean = average. The format of the function in Matlab is M = mean(A,dim). A will be the matrix you take the average (or mean) of, M will be the place where the results are going to go. If dim = 1, you will get a row vector with each element being the average of a column. If dim = 2 (as it is in your case), then you should get a column vector, with each element being the average of a row. Be careful, though, because at the end of your code you have ', which means transpose. That means that your column vector will be transformed into a row vector.

OK, so your code:

x(:,i) = mean( (y(:,((i-1)*j+1):i*j)), 2 )';

Start with the bit inside, that is

y(:,((i-1)*j+1):i*j)

So that is saying

matrix y(r,c)

where

r (row) is :, that is, all rows

c (column) is ((i-1)j+1):ij, that is, columns going from (i-1)j+1 until ij

Your code will then get the matrix resulting from that, which I called y(r,c), and will do the following:

mean( (y(r,c), 2 )

so get the result from above and take the mean (average) of each row. As your code has the ' afterwards, that is, you have:

mean( (y(r,c), 2 )'

then it will get the column vector and transform into a row vector. Each element of this row will be the average of a row of y(r,c).

Finally:

x(:,i) = mean( (y(r,c), 2 )';

means that the result of the above will be put in column i of matrix x.

Shouldn't this be x(i,:) instead?


The i-th column of the array x is the average of the i-th group of j columns of the array y.

For example, if i is 1 and j is 3, the 1st column of x is the average of the first three columns of y.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜