save loop data with non integer steps

조회 수: 2 (최근 30일)
Jerry
Jerry 2023년 2월 21일
댓글: Jerry 2023년 2월 21일
I have following script and I am trying to save the results of the loop in an array. I get following error message:
Array indices must be positive integers or logical values.
How can I save the results in an array even though my steps are not integer values?
reduced_scattering_coeff = 10;
absorbing_coeff = 1;
source_detector_distance= 1.5;
refection_parameter= 3.25;
pi = pi;
detectorsignal=zeros(size(0.01:0.01:1));
for absorbing_coeff = 0.01:0.01:1
mu_t = reduced_scattering_coeff+absorbing_coeff;
mu_eff = sqrt(3*(reduced_scattering_coeff+absorbing_coeff));
r1 = sqrt(((1/mu_t)^2)+source_detector_distance);
r2 = sqrt((((4/3*refection_parameter+1)/mu_t)^2)+(source_detector_distance^2));
detectorsignal= (1/(4*pi*mu_t))*((mu_eff+1/r1)*((exp(mu_eff*r1))/r1^2)+((4/3*refection_parameter)+1))*(mu_eff+1/r2)*((exp(-mu_eff*r2))/(r2^2));
detectorsignal(absorbing_coeff)= detectorsignal;
end

채택된 답변

Torsten
Torsten 2023년 2월 21일
reduced_scattering_coeff = 10;
absorbing_coeff = 1;
source_detector_distance= 1.5;
refection_parameter= 3.25;
absorbing_coeff = 0.01:0.01:1;
detectorsignal = zeros(size(absorbing_coeff));
for i=1:numel(absorbing_coeff)
mu_t = reduced_scattering_coeff+absorbing_coeff(i);
mu_eff = sqrt(3*(reduced_scattering_coeff+absorbing_coeff(i)));
r1 = sqrt(((1/mu_t)^2)+source_detector_distance);
r2 = sqrt((((4/3*refection_parameter+1)/mu_t)^2)+(source_detector_distance^2));
detectorsignal(i)= (1/(4*pi*mu_t))*((mu_eff+1/r1)*((exp(mu_eff*r1))/r1^2)+((4/3*refection_parameter)+1))*(mu_eff+1/r2)*((exp(-mu_eff*r2))/(r2^2));
end
plot(absorbing_coeff,detectorsignal)
  댓글 수: 1
Jerry
Jerry 2023년 2월 21일
this works perfectly, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2023년 2월 21일
acvalues = 0.01:0.01:1;
detectorsignal=zeros(size(acvalues));
for ind = 1:numel(acvalues)
absorbing_coeff = acvalues(ind);
% stuff. you should get the idea...
detectorsignal(ind)= (1/(4*pi*mu_t))*((mu_eff+1/r1)*((exp(mu_eff*r1))/r1^2)+((4/3*refection_parameter)+1))*(mu_eff+1/r2)*((exp(-mu_eff*r2))/(r2^2));
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by