How to exclude certain values from an array?

16 ビュー (過去 30 日間)
Hannah Goold
Hannah Goold 2020 年 11 月 16 日
コメント済み: Hannah Goold 2020 年 11 月 16 日
I am trying to plot a log equation, before I can I need to calculate x.
x can sometimes be returned as 0 or 1 so I need to exclude these points before I plot the log graph.
How do I achieve this?
So far I have tried this fairly inefficient way, and it doesnt seem to be excluding them from the array just not plotting them on the graph so that when i get to the last function to calculate p it doesnt calculate it for the whole array just one value:
xi1 = hits1./tot1
yi1=log(xi1./(1-xi1));
xplot1 = xi1
yplot1 = yi1;
xplot1(xplot1==0) = NaN;
yplot1(yplot1==0) = NaN;
xplot1(xplot1==1) = NaN;
yplot1(yplot1==1) = NaN;
xplot1(xplot1==-Inf) = NaN;
yplot1(yplot1==-Inf) = NaN;
xplot1(xplot1==Inf) = NaN;
yplot1(yplot1==Inf) = NaN;
figure(1)
plot(xplot1, yplot1,'linestyle','none','marker','o')
hold on
p1 = 4.8422;
p2 = -2.4067;
y = (p1.*xi1) + p2
plot(xi1, y)
hold off
p = 1/(1+exp(-((p1.*xi1) + p2)))
figure(3)
plot(xi1, p)

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 16 日
編集済み: Ameer Hamza 2020 年 11 月 16 日
If you want to remove them then use [] instead of NaN
xi1 = hits1./tot1
yi1=log(xi1./(1-xi1));
xplot1 = xi1
yplot1 = yi1;
mask = (xplot1==0) | (xplot1==1) | (isinf(xplot1));
xplot1(mask) = [];
yplot1(mask) = [];
figure(1)
plot(xplot1, yplot1,'linestyle','none','marker','o')
hold on
p1 = 4.8422;
p2 = -2.4067;
y = (p1.*xi1) + p2
plot(xi1, y)
hold off
p = 1/(1+exp(-((p1.*xi1) + p2)))
figure(3)
plot(xi1, p)
I haven't written a separate mask for yplot1 because I assume both have invalid elements at matching locations. If not, then you can similarly write a mask for yplot1
  1 件のコメント
Hannah Goold
Hannah Goold 2020 年 11 月 16 日
Thank you for your help! This works perfectly.

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

その他の回答 (1 件)

Rik
Rik 2020 年 11 月 16 日
Your current code is removing values from either the x or the y, instead of removing pairs. What you can do is define a logical array and mark all invalid positions. Then you can can remove the invalid point pairs (or set them to NaN, as you did).
For the last calculation you forgot to put in the dot for an element by element division.
  1 件のコメント
Hannah Goold
Hannah Goold 2020 年 11 月 16 日
Thank you!! I will work on adding the logical array now. Also thank you for finding the mistake in p!

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

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by