finding a specific element based on conditions

조회 수: 2 (최근 30일)
Hamid
Hamid 2022년 6월 29일
편집: Johan 2022년 6월 29일
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

채택된 답변

Johan
Johan 2022년 6월 29일
편집: Johan 2022년 6월 29일
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)
ans = 1.1317e-05
fun = @() realarray(delta_teta);
timeit(fun)
ans = 3.3761e-06
all(realarray(delta_teta) == realloop(delta_teta))
ans = logical
1
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))
ans = 120
reflected_muons=nnz(~x);
ratio_of_reflected = reflected_muons ./ numel(K);
AB1= array2table([K(:), r(:), disp(:), delta_teta(:) ])
AB1 = 70×4 table
Var1 Var2 Var3 Var4 ____ ______ _______________ ______________ 30 1.1353 1.1353-2.5595i 90-88.924i 40 1.3378 1.3378-2.4598i 90-78.443i 50 1.5251 1.5251-2.3482i 90-69.704i 60 1.7024 1.7024-2.223i 90-61.995i 70 1.8723 1.8723-2.0819i 90-54.909i 80 2.0369 2.0369-1.9212i 90-48.165i 90 2.1972 2.1972-1.7356i 90-41.526i 100 2.3542 2.3542-1.5159i 90-34.727i 110 2.5084 2.5084-1.2441i 90-27.364i 120 2.6604 2.6604-0.87299i 90-18.479i 130 2.8105 2.5674+0i 85.036+0i 140 2.9591 2.002+0i 71.129+0i 150 3.1062 1.7614+0i 64.346+0i 160 3.2522 1.5979+0i 59.425+0i 170 3.3971 1.4735+0i 55.511+0i 180 3.5411 1.3733+0i 52.252+0i
p=table (K',r',disp',delta_teta')
p = 70×4 table
Var1 Var2 Var3 Var4 ____ ______ _______________ ______________ 30 1.1353 1.1353+2.5595i 90+88.924i 40 1.3378 1.3378+2.4598i 90+78.443i 50 1.5251 1.5251+2.3482i 90+69.704i 60 1.7024 1.7024+2.223i 90+61.995i 70 1.8723 1.8723+2.0819i 90+54.909i 80 2.0369 2.0369+1.9212i 90+48.165i 90 2.1972 2.1972+1.7356i 90+41.526i 100 2.3542 2.3542+1.5159i 90+34.727i 110 2.5084 2.5084+1.2441i 90+27.364i 120 2.6604 2.6604+0.87299i 90+18.479i 130 2.8105 2.5674+0i 85.036+0i 140 2.9591 2.002+0i 71.129+0i 150 3.1062 1.7614+0i 64.346+0i 160 3.2522 1.5979+0i 59.425+0i 170 3.3971 1.4735+0i 55.511+0i 180 3.5411 1.3733+0i 52.252+0i
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개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by