[英]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 book. 我在书中找不到它。 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. 对于意
mean
, :
和'
咨询MATLAB的帮助。
It would help if you said exactly what you don't understand, but here are a few tips: 如果您确切地说出您不了解的内容,这会有所帮助,但是这里有一些提示:
if you have something like a(r,c), that means matrix a, row r, column c (always in this order). 如果您有a(r,c)之类的东西,则意味着矩阵a,行r,列c(始终按此顺序)。 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.
换句话说,括号内应有两个元素,并用逗号分隔,其中第一个表示行,第二个表示列。
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.
因此,如果您有一个(r,:),那么您将拥有矩阵a,行r,所有列。
If : is not alone in one of the sides of the comma, then it will mean "to". 如果:不在逗号的任一侧,则表示“ to”。 So if you have a(r, z:y), that means matrix a, row r, columns z to y.
因此,如果您有a(r,z:y),则意味着矩阵a,行r,列z到y。
Mean = average. 平均值=平均值。 The format of the function in Matlab is M = mean(A,dim).
Matlab中函数的格式为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.
A将是您取平均值(或均值)的矩阵,M将是结果将要经过的地方。 If dim = 1, you will get a row vector with each element being the average of a column.
如果dim = 1,您将获得一个行向量,每个元素是一列的平均值。 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.
如果dim = 2(按您的情况),则应获得列向量,每个元素为一行的平均值。 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) 矩阵y(r,c)
where 哪里
r (row) is :, that is, all rows r(行)为:,即所有行
c (column) is ((i-1) j+1):i j, that is, columns going from (i-1) j+1 until i j c(列)为((i-1) j + 1):i j,即从(i-1) j + 1到i j的列
Your code will then get the matrix resulting from that, which I called y(r,c), and will do the following: 然后,您的代码将获得由此产生的矩阵,我将其称为y(r,c),并将执行以下操作:
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).
该行的每个元素将是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. 表示将上述结果放入矩阵x的第i列。
Shouldn't this be x(i,:) instead? 难道不是x(i,:)吗?
The i-th column of the array x is the average of the i-th group of j columns of the array y. 数组x的第i列是数组y的第j列的第i组的平均值。
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. 例如,如果i为1,j为3,则x的第一列是y的前三列的平均值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.