How do you turn a vector of random positive and negative numbers and another vector with the counts into a histogram?
1 view (last 30 days)
Show older comments
I made two vectors in one matrix called pos(2,n), the first vector, pos(1,n) is a 'randn' vector with a normal distribution of random numbers, and the other, pos(2,n), is a value of either 1 or zero for hit or miss. The purpose is to generate Monte Carlo style rejection criteria for a normal distribution of random values and accept values less than a certain rejection criteria. Ex, pos(1,i) = -0.153, another random number is generated between 0 and 1 for this position, if that second random number is less than Rejection=0.80, then 1 gets added to the corresponding pos(2,i) position. I need to turn this matrix pos(2xn) into a histogram of the number of counts (pos(2,i)) compared to the position of that count (pos(1,i)). I have tried to use a combination of unique(), accumarray(), hist counts,and bins, but it gets an error because the pos(1,:) values can be non-integer and not positive. Please see my code below.
n_L_photons=12345;
FluorYld = 0.764;
i = 1;%
miss = 0;
format long
n_photon_pack = floor(n_L_photons/1e3);%photon packs of 1Mil
n_photon_rmdr = n_L_photons-n_photon_pack*1e3;%leftover photons not counted in 1M packs
pos=zeros(2,n_L_photons);
prob_photon = zeros(1,n_L_photons);
p = 1;
while i <= n_photon_pack
while p <= i*1e3
pos(1,p) = randn(1);
prob_photon(1,p) = rand(1);%random number between 0 and 1 created
if prob_photon(1,p) < FluorYld %if that rand number is less than the fluorescent yield, it will be absorbed and emitted
pos(2,p) = pos(2,p) + 1;%if it is emitted, a count is added to that positions 2nd row
else
miss = miss + 1;%this counts photons that were not absorbed or emitted.
end
p = p+1;%moves on to next random position
end
i = i+1;%moves on to next photon pack
end
k = 1e3*n_photon_pack+1;%start at the end of the last photon pack
while k <= n_photon_pack*1e3+n_photon_rmdr
prob_photon(1,k) = rand(1);
if prob_photon(1,k) < FluorYld
pos(2,k) = pos(2,k) + 1;
else
miss = miss + 1;
end
k = k+1;
end
b= length(pos);
for i = 1:b
if pos(2,i) == 0
pos(:,i) = NaN;
end
end
NaNCols = any(isnan(pos));
pos = pos(:,~NaNCols);
posT = pos';
u = unique(posT);
pos_to_integer = int32(1e5*(posT(:,1)));
A = accumarray(pos_to_integer,posT(:,2));
filtered_pos = [(u/1e5);A];
u=unique(filtered_pos(1,:));
[n,bin]=histcounts(filtered_pos(1,:),u);
for v=find(n>1).',
idx=find(bin==v)
end
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!