フィルターのクリア

I want to change the color of the markers in scatter plot which are below the function line

4 ビュー (過去 30 日間)
I have the following code
clc
clear
close all
d = 4;
N = 1000;
X = linspace(0,d,N);
func = @(X) 2*X.*cos(X.^2).*exp(sin(X.^2)) + 14;
limit = func(X);
x = 4*rand(1,N);
y = 25*rand(1,N);
figure(1),hold on
h = scatter(x,y,10,'markerfacecolor','b');
plot(X,limit,'k-','linew',1.3)
for i = 1:N
if any(y(i) < limit)
set(h,'YData','MarkerFaceColor','r','Markeredgecolor','r')
end
end
but it keeps giving the following error
Error using matlab.graphics.chart.primitive.Scatter/set
Invalid parameter/value pair arguments.
Any help would be appreciated
  2 件のコメント
Haseeb Hashim
Haseeb Hashim 2022 年 4 月 11 日
Sorry I posted it twice But please help me on this do I need to plot in a loop for this or is there are another solution

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

採用された回答

Star Strider
Star Strider 2022 年 4 月 11 日
d = 4;
N = 1000;
X = linspace(0,d,N);
func = @(X) 2*X.*cos(X.^2).*exp(sin(X.^2)) + 14;
limit = func(X);
x = 4*rand(1,N);
y = 25*rand(1,N);
figure(1)
hold on
scatter(x,y,10,'markerfacecolor','b');
Lv = y < func(x); % Logical Vector Based On 'func' Value At Each 'x'
scatter(x(Lv),y(Lv),10,'MarkerFaceColor','r','Markeredgecolor','r')
plot(X,limit,'k-','linew',1.3)
.
  2 件のコメント
Haseeb Hashim
Haseeb Hashim 2022 年 4 月 11 日
Hi Great work. Can you please explain these lines expecially the first one
Lv = y < func(x);
scatter(x(Lv),y(Lv),10,'MarkerFaceColor','r','Markeredgecolor','r')
Star Strider
Star Strider 2022 年 4 月 11 日
Thank you!
Sure!
The ‘Lv’ variable is a logical vector that calculates the value of ‘func’ at each ‘x’ value and compares that result with the corresponding ‘y’ value. If that ‘y’ value is less than ‘func(x)’, that value of ‘Lv’ is set to true. (It definitely helps that ‘func’ is an anonymous function in your original code, making this straightforward.)
The scatter call just after that uses ‘Lv’ to refer to the individual ‘x’ and ‘y’ values, plotting only those that are true as defined by ‘Lv’.

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

その他の回答 (1 件)

MJFcoNaN
MJFcoNaN 2022 年 4 月 11 日
You can try this:
clc
clear
close all
d = 4;
N = 1000;
X = linspace(0,d,N);
func = @(X) 2*X.*cos(X.^2).*exp(sin(X.^2)) + 14;
limit = func(X);
x = 4*rand(1,N);
y = 25*rand(1,N);
ind_r = y < limit;
c=zeros(N,3);
% red
c(ind_r, 1)=1;
% blue
c(~ind_r, 3)=1;
figure(1),hold on
h = scatter(x,y,10,c);
plot(X,limit,'k-','linew',1.3)
  2 件のコメント
Haseeb Hashim
Haseeb Hashim 2022 年 4 月 11 日
Now I get it we cant check the particular random against every number of function value because in this case the random numbers greater than the greatest number in function values will be turned blue otherwise they will be all read
MJFcoNaN
MJFcoNaN 2022 年 4 月 11 日
Why do you make both x and y random?
If your task allows one random value, for example x=X, it may give out a more "reasonable" result, which achieves the same algorithm as the other answer.

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

カテゴリ

Help Center および File ExchangeTwo y-axis についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by