2017-04-07 1 views
0
X(100,371) 
%% contains 100 datapoints for 371 variables 

평균 + 표준 편차 : 평균 표준 편차 이내의 데이터 만 유지하려고합니다. 내가 잘못 뭐하는 거지내 논리 마스크가 matlab의 2D 매트릭스에서 제대로 작동하지 않는 이유는 무엇입니까?

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. 

:

이 내가 진행하고 어떻게?

답변

3

라인

test(g) = X(g); 

컴파일러가 X(g)이 그것을 test(g)을 실행 때 g 1을 표시하는 X 모든 요소를 ​​반환하고, 할당에서 실행과 구문 문제의 조금이있다 test 변수는 g (1x37100)으로 색인을 생성 할 수있는 크기로 만들어지고 모든 요소를 ​​올바른 위치에 할당합니다. 당신이 뭔가를 추가 할 수있는 할당 전에 짧은 긴 이야기 :

test = zeros(size(X)); 

우리는이에있는 동안, 당신은 R2016b 또는 최근에

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

repmat 할 필요없이 논리적 인덱스를 얻을 수 bsxfun을 사용할 수 있습니다 암시적인 bsxfun 확장이 있습니다.

g = X > mx - sx & X < mx + sx 
관련 문제