Computing mean of selected matrix rows, excluding values below a certain threshold
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!