Is there a vectorized way to work with a different number of values ​​for one column in MATLAB?

In MATLAB, is there a more concise way to handle discrete conditional column indexing than using a for loop? Here is my code:

x=[1 2 3;4 5 6;7 8 9];
w=[5 3 2];

q=zeros(3,1);
for i = 1:3
    q(i)=mean(x(x(:,i)>w(i),i));
end
q

My goal is to take the average of the top x% of the set of values ​​for each column. The above code works, but I'm just wondering if there is a more concise way to do this?

+3
source share
3 answers

, PRCTILE, , Toolbox. , , NANMEAN. x, w , NaN BSXFUN, NANMEAN:

x(bsxfun(@le,x,w)) = nan;
q = nanmean(x);
+1

, , . , for, y, x.

x=[1 2 3;4 5 6;7 8 9];
w=[5 3 2];

y = x > repmat(w,size(x,1),1);
q = sum(x.*y) ./ sum(y)

, .

+1

: , x% .

%# make up some data
data = magic(5);

%# find out how many rows the top 40% are
nRows = floor(size(data,1)*0.4);

%# sort the data in descending order
data = sort(data,1,'descend');

%# take the mean of the top 20% of values in each column
topMean = mean(data(1:nRows,:),1);
+1

Source: https://habr.com/ru/post/1765387/


All Articles