2D Surface Random Number Plot
Show older comments
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
More Answers (2)
Steven Lord
on 15 Dec 2022
2 votes
I recommend using histogram2.
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)
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 Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

