2D Surface Random Number Plot

7 views (last 30 days)
Syed Arafun Nabi
Syed Arafun Nabi on 15 Dec 2022
Commented: Syed Arafun Nabi on 15 Dec 2022
The 2D square surface has 1m length in each side and splitted into 10 grid(Total grid is 10x10). A ball is hitting the surface 1000 times. The hit is random. The exact position of each hit is determined by random number. How can I find the total hit count in each grid? Each grid will be named as
A0101, A0201 . . . .A1001
A0102, A0202 . . . .A1002
.........................................
A1001, A1002 . . . A1010
Initial codes are as followings -
x = rand;
y = rand;
x1 = linspace(0,1,11)
y1 = linspace(0,1,11)
for i = 1:1000
[x,y];
end

Accepted Answer

KSSV
KSSV on 15 Dec 2022
% Make grid
x = linspace(0,1,11) ;
y = linspace(0,1,11) ;
[X,Y] = meshgrid(x,y) ;
% Get mid points of each element
mx = linspace(0.05,0.95,10) ;
my = linspace(0.05,0.95,10) ;
[MX,MY] = meshgrid(mx,my) ;
plot(X,Y,'.r',MX,MY,'.b')
MZ = zeros(size(MX)) ; % Initialize the hit count
for i = 1:1000
hit = rand(1,2) ;
% Check where it is hit
idx = knnsearch([MX(:) MY(:)],hit) ;
% UPdate the count
MZ(idx) = MZ(idx)+1 ;
end
figure
hold on
plot(X,Y,'k',X',Y','k')
scatter(MX(:),MY(:),[],MZ(:),'filled') ;
colormap(jet) ;
cb = colorbar ;
ylabel(cb,'Number of hits')
  3 Comments
KSSV
KSSV on 15 Dec 2022
Z is in matrix already you can plot a contour.
About boundary, we need to change the strategy.
Syed Arafun Nabi
Syed Arafun Nabi on 15 Dec 2022
If the probablity of hitting the bondary is 20% & inside the surface is 80%. The boundary hit will be consider in the corresponding grid. How can we modify? Can you please assist?
My actual code is more than 250 lines. so this is just a template
a = rand
a = 0.8406
if a<.05
%hit y=0 plane
x=rand
y=0
elseif a<.10
%hit y=0 plane
x=rand
y=1
elseif a<.15
%hit x=0 plane
x=0
y=rand
elseif a<.20
%hit x=1 plane
x=1
y=rand
else
%inside the surface
x=rand
y=rand
end
x = 0.4815
y = 0.4564

Sign in to comment.

More Answers (2)

Steven Lord
Steven Lord on 15 Dec 2022
I recommend using histogram2.

Fifteen12
Fifteen12 on 15 Dec 2022
Edited: Fifteen12 on 15 Dec 2022
The best way to do this is to use sequential indices, rather than row, column. Then you're just keeping track of a single number. If you do need the x, y coordinates (row, column), you can try this:
hit_count = zeros(10);
hits = randi(numel(hit_count), 1, 1000); %Randomly hit an cell index between 1 and 100, 1000 times
for i = 1:10
for j = 1:10
seq_index = (i-1) * 10 + j;
hit_count(i, j) = sum(hits == seq_index);
end
end
This would be the same as doing the following:
hit_count = zeros(10);
hits = randi(numel(hit_count), 1, 1000); %Randomly hit an cell index between 1 and 100, 1000 times
for i = 1:numel(hit_count)
hit_count(i) = sum(hits == i);
end
As for the name of the cells, I'm not sure how you want to access them. You can easily make up a calling function though that returns the hits per cell based on an inputted name though...
hits_at_3_5 = getNumberOfHitsPerCell('A0305', hit_count);
disp(hits_at_3_5)
8
function [num_hits] = getNumberOfHitsPerCell(cell_name, hit_count)
x = str2num(cell_name(2:3));
y = str2num(cell_name(4:5));
num_hits = hit_count(x, y);
end
You'd probably want to do some more intense string validation if you go this route though. Good luck!

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!