How to generate all the possible data points in this 2D triangle?

1 回表示 (過去 30 日間)
Salad Box
Salad Box 2023 年 1 月 18 日
編集済み: Salad Box 2023 年 1 月 18 日
Hi,
I have a blue triangle in a 2-D V-S space.
How can I know the [s v] values of all the possible points within the blue triangle area?
  4 件のコメント
Torsten
Torsten 2023 年 1 月 18 日
s >=0
v >= 0
s + v <= 1
The blue region is characterized as the set of pairs (s,v) that satisfy all three constraints.
Never heard of linear programming ? Simplex algorithm ? Optimization under constraints ? Linprog ?
Salad Box
Salad Box 2023 年 1 月 18 日
編集済み: Salad Box 2023 年 1 月 18 日
Sorry I am a beginner to Matlab and programming in general. I haven't heard of any of those terms before but I will find out.
But I think now I understand your algorithm/conditions.
I wrote something based on the logic you put down here.
s = linspace(0,1,20);
v = linspace(0,1,20);
sv = cartprod(s,v); % generate all the points in the square
% condition
idx = (sv(:,1) + sv(:,2))<=1; % s+v<=1
idx = find(idx == 1);
sv = sv(idx,:);
figure
plot(sv(:,1), sv(:,2),'.r')
and I plotted the sv it also worked perfectly.
I would like to thank you for your advice and its really valuable to me.

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

採用された回答

KSSV
KSSV 2023 年 1 月 18 日
P = [0 0 ; 0 1 ; 1 0] ; % vertices of triangle
m = 20 ; n = 20 ;
x = linspace(min(P(:,1)),max(P(:,1)),m) ;
y = linspace(min(P(:,2)),max(P(:,2)),n) ;
[X,Y] = meshgrid(x,y) ;
idx = inpolygon(X,Y,P(:,1),P(:,2)) ;
patch(P(:,1),P(:,2),'b')
hold on
plot(X(idx),Y(idx),'.r')
  1 件のコメント
Salad Box
Salad Box 2023 年 1 月 18 日
That is perfect. All I need to do is to modify the number 20 to another number depends on how many points I want. Thank you for your answer.:)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by