Why is my logical mask not working on a 2D matrix in matlab properly?

Multi tool use
Multi tool use


Why is my logical mask not working on a 2D matrix in matlab properly?


X(100,371)
%% contains 100 datapoints for 371 variables



I want to keep only the data which are within mean+standard deviation:mean-standard deviation.



This is how I am proceeding:


mx=mean(X);sx=std(X);
%%generate mean, std
%%this generates mx(1,371) and sx(1,371)

mx=double(repmat(mx,100,1));
%%this fills a matrix with the same datapoints,
%%100 times
sx=double(repmat(sx,100,1));
%% this gives mx(100,371) and sx(100,371)


g=X>mx-sx & X<mx+sx;
%%this creates a logical mask g(100,371)
%%filled with 1s and 0s

test(g)=X(g);
%%this should give me test(100,371), but I get
%%test(37100), which is wrong as it doesnt maintain
%%the shape of X

test=reshape(test,100,371)
%% but when I compare this to the my original matrix
%% X(100,371) I hardly see a difference (datapoints
%% in test are still outside the range I want.



What am I doing wrong?




1 Answer
1



There is just a little bit of syntax issue with the line


test(g) = X(g);



When the compiler executes X(g) it returns all the elements in X for which g indicates 1, and at the assignment when it executes test(g) it creates a test variable good enough in size to be indexed by g which is 1x37100 and then assigns all the elements at the right places. Long story short before the assignment you can add something like:


X(g)


X


g


test(g)


test


g


test = zeros(size(X));



While we are at this, you could use bsxfun to get the logical indexing without having to do repmat


g = bsxfun(@gt,X,mx - sx) & bsxfun(@lt,X,mx + sx)



In R2016b or recent there is implicit bsxfun expansion


g = X > mx - sx & X < mx + sx






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

HyEBOGgh
ZdlFu3cfCM8,ggqiXzsga IzL QJ9bXSkbb Ml4VtZp,A fBww7 f8OdqG,aaiItt UcYHY1r WgtgMhv

Popular posts from this blog

Rothschild family

Cinema of Italy