How can I index non-integer values
显示 更早的评论
the code is as under: (all others are constants) I need to generate a matrix of PR of 1000 different values of R.
----------------------------------------------
PR=(1:1001);
for R=990:0.02:1010
PR(R-989)=(pt*(g^2)*sigma*(lambda^2))/(((4*pi)^3)*R^4);
end
ERROR:
Attempted to access PR(1.02); index must be a positive integer or logical.
回答(2 个)
Bjorn Gustavsson
2012-2-8
Since PR is an array it has n values at indices from 1 to n. If you want to get values at other "indices" you can turn to interpolation:
nr_you_want_out = 1001;
PRinterp = interp1(1:n,PR,linspace(1,n,nr_you_want_out));
HTH
Walter Roberson
2012-2-8
PR=(1:1001);
Rv=990:0.02:1010;
for Ridx = 1 : length(Rv)
R = Rv(Ridx);
PR(Ridx)=(pt*(g^2)*sigma*(lambda^2))/(((4*pi)^3)*R^4);
end
Alternate solution with no loop:
R = 990:0.02:1010;
PR = pt .* (g.^2) .* sigma .* (lambda.^2) ./ (4*pi).^3 ./ (R.^4);
类别
在 帮助中心 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!