Delete data with some requirements
조회 수: 5 (최근 30일)
이전 댓글 표시
I have a coding like this:
Zx= zeros(size(X));
Zy=zeros(size(Y));
for i=1:length(X);
if Zx(i)<2.5 && Zx(i)>-2.5 && Zy(i)<2.5 && Zy(i)>-2.5
Zx(i) = (X(i) - Mean_X)/Std_X;
Zy(i) = (Y(i) - Mean_Y)/Std_Y;
else
Zx(i) && Zy(i) == 0
end
end
rowsToDelete = (Zx < -2.5 | Zx > 2.5) & (Zy <= -2.5 & Zy >= 2.5);
Xscore(rowsToDelete) = []; % Set to null.
Yscore(rowsToDelete) = []; % Set to null.
OutZscore=[X Y Zx Zy];
I also attach my data, two first coloumn is X and Y and two second column is Zx and Zy. X and Zx is a partner and Y and Zy is partner to in the same row.
I want to delete my X dan Y data with the requirement is X data wil be deleted is the requirement will explain such as (Zx<-2.5 and Zx>2.5) and (Zy<-2.5 and Zy>2.5).
The result is not satified.
I want to plot the data to compare the process before and after deleted.
Is there any one can help me I would be appreciate.
Thx
댓글 수: 5
Joel Handy
2019년 7월 16일
I think maybe you are looking for:
X = X(rowsToKeep);
Y = Y(rowsToKeep);
Zx = Zx(rowsToKeep);
Zy = Zy(rowsToKeep);
Or
X(~rowsToKeep) = [];
Y(~rowsToKeep) = [];
Zx(~rowsToKeep) = [];
Zy(~rowsToKeep) = [];
Not that in the latter example, you arent setting anything to null, you are setting them to "empty", i.e. deleting them.
Also as far as plotting, Star Striders answers should be what you are looking for.
채택된 답변
Star Strider
2019년 7월 16일
Try this:
D = load('Xsample_data.txt');
X = D(:,1);
Y = D(:,2);
Zx = D(:,3);
Zy = D(:,4);
Lvx = (Zx < -2.5) & (Zx > 2.5);
Lvy = (Zy < -2.5) & (Zy > 2.5);
RowsToDelete = Lvx & Lvy;
figure
plot(X, Y)
hold on
plot(Zx, Zy)
hold off
grid
figure
plot(X(~RowsToDelete), Y(~RowsToDelete))
hold on
plot(Zx(~RowsToDelete), Zy(~RowsToDelete))
hold off
grid
댓글 수: 3
Star Strider
2019년 7월 16일
Try this:
Lvx = (Zx < -2.5) | (Zx > 2.5);
Lvy = (Zy < -2.5) | (Zy > 2.5);
RowsToDelete = Lvx & Lvy;
figure
plot(X, Y, '-pb')
hold on
plot(X(~RowsToDelete), Y(~RowsToDelete), '-pr')
hold off
grid
Experiment with the plot to get the result you want.
Star Strider
2019년 7월 16일
‘How about the value of Zx and Zy is above 3 maybe in other case. It should be also deleted.’
Set the upper limit to 3:
Lvx = (Zx < -2.5) | (Zx > 2.5);
Lvy = (Zy < -2.5) | (Zy > 2.5);
That will change the upper limit, so that only values >3 (and <-2.5) will be deleted. All values >3 will already be deleted with the current threshold of >2.5.
추가 답변 (1개)
Skydriver
2019년 7월 16일
댓글 수: 4
Joel Handy
2019년 7월 16일
There is a bit of a language barrier so I'm not entirely sure what your question is. The statement you have highlighted will be true when -2.5 <= Zx <=2.5 AND -2.5 <= Zy <=2.5 and rowsToKeep with be the same size as Zx and Zy. You can craft a similar logical statement with whatever conditions you would like. If you would rather the upper limit be 3, the statment woul look like this:
rowsToKeep = (Zx >= -2.5 & Zx <= 3) & (Zy >= -2.5 & Zy <= 3)
I'm using logical indexing in my answer. That is one topic to research for more information.
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!