How to assign 0 or 1 randomly to the coordinates?

조회 수: 4 (최근 30일)
Mehrdad Nasirshoaibi
Mehrdad Nasirshoaibi 2023년 2월 21일
댓글: Voss 2023년 2월 21일
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(:)]
P = 100×2
-2.0000 -2.0000 -2.0000 -1.2222 -2.0000 -0.4444 -2.0000 0.3333 -2.0000 1.1111 -2.0000 1.8889 -2.0000 2.6667 -2.0000 3.4444 -2.0000 4.2222 -2.0000 5.0000
My question is that how can I assign only 0 or 1 to each coordinate randomly?
Thank you in advance.

채택된 답변

Star Strider
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
c = 10×10 logical array
1 0 0 0 1 1 0 1 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1 0 1 0 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 0 1 0 1 1 1 0 0 1 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 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.
.
  댓글 수: 2
Mehrdad Nasirshoaibi
Mehrdad Nasirshoaibi 2023년 2월 21일
Awesome. Thank you so much.
Star Strider
Star Strider 2023년 2월 21일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2023년 2월 21일
val = randi([0 1], size(X)) ;

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by