Computing mean of selected matrix rows, excluding values below a certain threshold

14 次查看(过去 30 天)
I have a 20 x 10 matrix of RTs and need to create a function with two inputs, the first specifying the matrix rows of interest, the second an RT threshold for values to be included in the computation. The output should be the mean of specified rows, excluding values below 0.7. I have created the following function:
function mymean = thresholdedmean(rownum, threshold)
load RT.mat;
x = RT; n0 = 0;
for i = rownum
n0 = n0+1;
mRTsth(n0) = mean(x(x(i,:) < threshold),2);
mymean = mRTsth;
end
Entering for example 'thresholdedmean([2, 4, 6, 9], 0.7)' returns four means, but they don't correspond to the means of these rows with values below 0.7 excluded. For reference, rows 1-9 of the matrix is below. The four means returned are 0.3379 0.3645 0.3421 0.3488 when I believe they should be 0.4163 0.3383 0.2749 0.3709.
I began with using the line mRTsth(n0) = mean(x(x(i,:) < threshold)); and later realised i needed to specify in which dimension i want to compute the mean, but adding ',2' before the final parenthesis didn't change the output.
RT matrix
0.207742 0.594896 0.963089 0.106762 0.182922 0.659605 0.291984 0.301455 0.644765 0.906308
0.301246 0.262212 0.546806 0.653757 0.239932 0.518595 0.431651 0.701099 0.376272 0.879654
0.470923 0.602843 0.521136 0.494174 0.886512 0.972975 0.015487 0.666339 0.190924 0.817761
0.230488 0.711216 0.231594 0.779052 0.028674 0.648991 0.984064 0.539126 0.428253 0.260728
0.844309 0.221747 0.488898 0.715037 0.489901 0.800331 0.167168 0.698106 0.482022 0.594356
0.194764 0.117418 0.624060 0.903721 0.167927 0.453798 0.106216 0.666528 0.120612 0.022513
0.225922 0.296676 0.679136 0.890923 0.978681 0.432392 0.372410 0.178132 0.589507 0.425259
0.170708 0.318778 0.395515 0.334163 0.712694 0.825314 0.198118 0.128014 0.226188 0.312719
0.227664 0.424167 0.367437 0.698746 0.500472 0.083470 0.489688 0.999080 0.384619 0.161485

采纳的回答

Michal
Michal 2019-11-14
编辑:Michal 2019-11-14
I think you mean that you would like to exclude values above the threshold? Your code and your expected means suggest this so I am going to assume that anything larger or equal to 0.7 should be excluded.
Great work on the code but you need to specify the row number as i and use the logical array (x(i,:) < threshold) as a column index:
mRTsth(n0) = mean(x(i,x(i,:) < threshold),2)
Alternatively, you can turn all the values larger or equal to threshold into NaNs and ask mean to omit NaNs. This way you do not have to use for loop at all:
x(x>=threshold) = NaN;
mRTsth = mean(x(rownum,:),2,'omitnan');
Hope this helps!
  3 个评论
Michal
Michal 2019-11-15
No worries, glad I could help!
As for what was going wrong - initially, since there was only one subscript (the logical array in this case) Matlab used linear indexing (see Linear Indexing here). Therefore, you were getting values column-wise:
x(x(2,:)<0.7) % the one subscript in this case is x(2,:)<0.7
ans =
0.2077 0.3012 0.4709 0.2305 0.8443 0.1948 0.2259 0.2277
Those are the values that you find if you go down the columns and exclude values based on the logical array x(2,:) < 0.7.
Hope this clears it up.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by