Delete data with some requirements

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 件のコメント

Jan
Jan 2019 年 7 月 16 日
編集済み: Jan 2019 年 7 月 16 日
Please use the Code-formatting for the code to improve the readability.
What is the purpose of this line:
Zx(i) && Zy(i) == 0
? Do you mean:
Zx(i) = 0;
Zy(i) = 0;
What is Mean_X and Std_X? What is Zxt?
After you have set
Zx = zeros(size(X));
Zy = zeros(size(Y));
checking the values of Zx and Zy is not meaningful:
if Zxt(i)<2.5 && Zx(i)>-2.5 && Zy(i)<2.5 && Zy(i)>-2.5
You you want to check X and Y instead?
Xscore(rowsToDelete) = []; % Set to null.
The comment is misleading: This does not set the data to "null".
Skydriver
Skydriver 2019 年 7 月 16 日
編集済み: Skydriver 2019 年 7 月 16 日
I assume that Zx(i) and Zy (i) will be removed and I replace with 0.
Yess
Mean_X is mean value of data X (column 1) and Std_X is standar deviation value of data X as well on Mean Y and Std Y. I want do deetection the distribution data X and Y based on the position of mean and standardeviation.
Joel Handy
Joel Handy 2019 年 7 月 16 日
編集済み: Joel Handy 2019 年 7 月 16 日
its a little hard to tell exactly what you are doing since there seems to be some missing contex with a number of undefined variables. Also, in your description it sounds like your data has 4 columns but the sample data you provided only has three columns.
One problem I see, you set Zx and Zy to zero then check if the absolute value of Zx and Zy are less than 2.5 which will always be true. You will never run your "else" code.
I also think you want rowsToDelete to be abs(Zx) > 2.5 | abs(Zy) > 2.5. so that data gets deleted if Zx exceeds the limit OR Zy exceeds the limit. The way you have it written Zx AND Zy need to exceed the limit,
As a side note, I believe the code you have can be simplified significantly:
Zx = (X - mean(X))./std(X);
Zy = (Y - mean(Y))./std(Y);
rowsToKeep = abs(Zx) <= 2.5 & abs(Zy) <= 2.5;
OutZScore = [X(rowsToKeep) Y(rowsToKeep) Zx(rowsToKeep) Zy(rowsToKeep)];
Skydriver
Skydriver 2019 年 7 月 16 日
Thank you Joel Handy with your respond. I just put in the file of data input the out put of Zx and Zy. I want to show the result of my calculation so the row number 95th until the end wil be deleted. But I still don't know how to process for deleting. The requirement must be part of the data X and Y will be removed.
Joel Handy
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
Star Strider 2019 年 7 月 16 日

0 投票

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 件のコメント

Skydriver
Skydriver 2019 年 7 月 16 日
編集済み: Skydriver 2019 年 7 月 16 日
Dear Star Rider
I just want plot the data of X and Y and also the data X(after delete) and Y (after delete). I will delete both X, Y and Zx and Zy when fullfill the requirement in the same row offcourse. My point is actually to delete the data X and Y when Zx for X is beloow -2.5 and upper 2.5 as well Zy is bellow -2.5 and upper 2.5. Maybe I have mistake to define the null.
Star Strider
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
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
Skydriver 2019 年 7 月 16 日

0 投票

Dear Star Rider and Joel Handy.
I tried to combine your suggestion and the coding is like this:
D = load('Xsample_data.txt');
X = D(:,1);
Y = D(:,2);
Zx = D(:,3);
Zy = D(:,4);
Mean_X = mean(X)
Std_X = std(X)
Mean_Y = mean(Y)
Std_Y = std(Y)
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
rowsToKeep = abs(Zx > -2.5 | Zx < 2.5) & (Zy > -2.5 & Zy < 2.5);
OutZScore = [X(rowsToKeep) Y(rowsToKeep) Zx(rowsToKeep) Zy(rowsToKeep)];
X1=X(rowsToKeep);
Y1=Y(rowsToKeep);
figure
plot(X, Y,'b.')
hold on
plot(X1,Y1,'r.')
hold off
grid
If your any comment for my coding?

4 件のコメント

Joel Handy
Joel Handy 2019 年 7 月 16 日
The loop is unecessary. I think so is the recomputing Zx and Zy since you are just recomputing the values in the data file?
I also suggest you look into the abs function which returns the absolute value (i.e negative values are made positive values and positive values are left alone).
D = load('Xsample_data.txt');
X = D(:,1);
Y = D(:,2);
Zx = D(:,3);
Zy = D(:,4);
Mean_X = mean(X)
Std_X = std(X)
Mean_Y = mean(Y)
Std_Y = std(Y)
rowsToKeep = abs(Zx) <= 2.5 & abs(Zy) <= 2.5;
Zx = (X - Mean_X)./Std_X; % I think this line is unecessary
Zx(~rowsToKeep) = 0;
Zy = (Y - Mean_Y)./Std_Y; % I think this line is unecessary
Zy(~rowsToKeep) = 0;
OutZScore = [X(rowsToKeep) Y(rowsToKeep) Zx(rowsToKeep) Zy(rowsToKeep)];
X1=X(rowsToKeep);
Y1=Y(rowsToKeep);
figure
plot(X, Y,'b.')
hold on
plot(X1,Y1,'r.')
hold off
grid
Skydriver
Skydriver 2019 年 7 月 16 日
rowsToKeep = abs(Zx) <= 2.5 & abs(Zy) <= 2.5;
How about the value of Zx and Zy is above 3 maybe in other case. It should be also deleted.
Joel Handy
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.
Joel Handy
Joel Handy 2019 年 7 月 16 日
I'm not away of any private comunication option

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2019 年 7 月 16 日

コメント済み:

2019 年 7 月 16 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by