finding a specific element based on conditions
Show older comments
Dear all,
I have two problems with my code.
1) The p table is filled in one row but I expected to have 100*4 table. as all variables have 100 rows.
2) I want to find a value in one column of my table if a condition in is satisfied in another column(both are in the same row). The condition is that if we reached to the last complex value in delta_teta column, I want to extract the value for K in the same row.
Please find a figure showing the problem more clearly attached.
Many thanks in advane.
K = generator(30,10,70);
a = 2.8;
B = 0.25;
r = radius_cal (K,B) ;
disp = disp_calc(B,K,a);
teta = atand( disp ./ a);
b = sqrt ((a .* a) + (disp .* disp)) ;
delta_teta = asind (b ./ (2 .* r)) .* 2;
for j = 1:numel(delta_teta)
x(j) = isreal(delta_teta(j));
end
reflected_muons=nnz(~x);
ratio_of_reflected = reflected_muons ./ numel(K);
AB1= array2table([K(:), r(:), disp(:), delta_teta(:) ]);
p=table (K,r,disp,delta_teta);
%colnames = {'Energy','Radius' ,'Displacement' ,'Δθ'};
writetable(AB1,'data.csv')
function r = radius_cal (K,B)
E0= my_const.m_mu_kg .* my_const.C .* my_const.C .* 6241506479963.2 ;
gamma = 1+ (K ./ E0);
v = my_const.C * sqrt (1- (1./(gamma .* gamma)));
omega= ((my_const.q .* B) ./ my_const.m_mu_kg) .* sqrt (1- (v .* v) ./ (my_const.C .* my_const.C) );
r= v ./ omega;
end
function k=generator(start,step,quantity)
k=start+step*(0:quantity-1);
end
function disp = disp_calc(B,K,a)
E0= my_const.m_mu_kg .* my_const.C .* my_const.C .* 6241506479963.2 ;
gamma = 1+ (K ./ E0);
v = my_const.C * sqrt (1- (1./(gamma .* gamma)));
omega= ((my_const.q .* B) ./ my_const.m_mu_kg) .* sqrt (1- (v .* v) ./ (my_const.C .* my_const.C) );
r= v ./ omega;
disp = r - sqrt ((r .* r) - (a .* a));
end
Accepted Answer
More Answers (0)
Categories
Find more on Gamma Functions 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!