finding a specific element based on conditions
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
Johan
el 29 de Jun. de 2022
Editada: Johan
el 29 de Jun. de 2022
1) Your data are row vectors and not column vectors which is why your table statement does not lead to what you expect. Transposing the array in your table call should fix it.
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;
Testing your imaginary numbers array wise is often more efficient but that's a detail considering the small size of your arrays
fun = @() realloop(delta_teta);
timeit(fun)
fun = @() realarray(delta_teta);
timeit(fun)
all(realarray(delta_teta) == realloop(delta_teta))
2) for this you can use find to get the index of your last imaginary value in delta_theta:
x = realarray(delta_teta);
i_index = find(not(x));
K(i_index(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')
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
function out = realloop(in)
out = zeros(1,numel(in));
for j = 1:numel(in)
out(j) = isreal(in(j));
end
end
function out = realarray(in)
out = imag(in)==0;
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Debugging and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!