finding a specific element based on conditions
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
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)
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!