Domanda

I have a question about matlab. It seems simple but I can't find the solution for inequalities linear region plot in matlab. For example, I want to plot the regions of y-x, and y>x and show the color for each one. For any x, y, but we can assume x = [-50:50].

Thank you.

I tried this one but don't know how to show the color for the third parameter.

[X,Y]=meshgrid(-1:0.01:1,-1:0.01:1); 
ineq1 = Y<X;
ineq2 = Y>X;
ineq3 = Y>-X;
colors = zeros(size(X))+ineq1+ineq2+ineq3;
scatter(X(:),Y(:),3,colors(:),'filled')
È stato utile?

Soluzione

[X,Y]=meshgrid(-1:0.01:1,-1:0.01:1); 

x1=reshape(X,[size(X,1)*size(X,2) 1]);
y1=reshape(Y,[length(x1) 1]);
a=[x1 y1];

ineq1=a(a(:,1)>a(:,2),:);
ineq2=a(a(:,1)<a(:,2),:);
ineq3=a(-a(:,1)<a(:,2),:);

scatter(ineq1(:,1),ineq1(:,2),3,'b','filled');
hold on;
scatter(ineq2(:,1),ineq2(:,2),3,'r','filled');
scatter(ineq3(:,1),ineq3(:,2),3,'g','filled');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top