I want to calculate the mean of two rows in matlab

前端 未结 3 1688
别跟我提以往
别跟我提以往 2021-01-29 05:40

I have a 1028 by 18 matrix in matlab.I want to calculate the mean of 1st and 2nd row by column values,3rd and 4th and so on in Matlab and get a new matrix with the mean values.<

3条回答
  •  再見小時候
    2021-01-29 05:41

    I think what you are looking for is:

    x = rand( 1028, 18 );
    meanx = ( x(1:2:end,:) + x(2:2:end,:)) / 2;
    

    After running this, meanx will be a [514 x 18] matrix.

    The first row of meanx is the average of rows 1 and 2 in x.

    The second row of meanx is the average of rows 3 and 4 in x.

    The third row of meanx is the average of rows 5 and 6 in x.

    EDIT:

    If you also want to exclude some of the rows from the averaging procedure based on the value of the first row, then you can add the following:

    dx = diff(x(:,1));
    
    goodrows = (dx(1:2:end) == 0);  %find row-pairs for which the first elements match
    badrows = ~goodrows;
    
    goodmeans = meanx(goodrows,:)  %average rows with matching first element
    badmeans = meanx(badrows,:)    %average rows with non-matching first element
    

提交回复
热议问题