2D Surface Random Number Plot
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Syed Arafun Nabi
am 15 Dez. 2022
Kommentiert: Syed Arafun Nabi
am 15 Dez. 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1231907/image.png)
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
0 Kommentare
Akzeptierte Antwort
KSSV
am 15 Dez. 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 Kommentare
KSSV
am 15 Dez. 2022
Z is in matrix already you can plot a contour.
About boundary, we need to change the strategy.
Weitere Antworten (2)
Fifteen12
am 15 Dez. 2022
Bearbeitet: Fifteen12
am 15 Dez. 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)
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!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!