How to assign 0 or 1 randomly to the coordinates?
1 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I defined a grid mesh as the follow;
x = linspace(-2,2,10) ;
y = linspace(-2,5,10) ;
[X,Y] = meshgrid(x,y) ;
plot(X,Y,'.r')
P = [X(:) Y(:)]
My question is that how can I assign only 0 or 1 to each coordinate randomly?
Thank you in advance.
0 个评论
采纳的回答
Star Strider
2023-2-21
I am not certain what you want to do.
Perhaps this —
x = linspace(-2,2,10) ;
y = linspace(-2,5,10) ;
[X,Y] = meshgrid(x,y) ;
c = randi([0 1], size(Y))==1
figure
plot(X(c),Y(c), '.r')
hold on
plot(X(~c), Y(~c), '.b')
hold off
axis('padded')
legend('1','0', 'Location','best')
The top row of ‘c’ correspoionds to -2 and the bottom row corresponds to +5. The columns are not flipped.
.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!