MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=6)
-   -   matlab if-else条件句改写 (https://www.labfans.com/bbs/showthread.php?t=6081)

elohim0807 2008-12-30 00:29

matlab if-else条件句改写
 
matlab if-else条件句改写

function [V,a]=sphshell(r,R1,R2,rho)

r=[7000000;5000000;1500000];
G=6.672e-11;
R1=3500000;
R2=6370000;
rho=4500;

for i=1:1:3
r(i)=r(i,1);
if r(i)>R2
V(i)=(4/3)*pi*G*rho*(R2^3-R1^3)./r(i);
a(i)=(4/3)*pi*G*rho*(R2^3-R1^3)./r(i).^2;
elseif R1<r(i),r(i)<R2;
V(i)=2*pi*G*rho*(R2^2-r(i).^2/3)-(4/3)*pi*G*rho*R1^3./r(i);
a(i)=(4/3)*pi*G*rho*(r(i).^3-R1^3)./r(i).^2;
else
V(i)=2*pi*G*rho*(R2^2-R1^2);
a(i)=0;
end
end

V=[V(1);V(2);V(3)];
a=[a(1);a(2);a(3)];
请问怎么把上面程序改写?不能使用循环(FOR,WHILE)和条件(IF/ELSE)语句.
题目没说能不能用SWICTH,假如也不用SWICTH,请问该程序能否被改写?
谢谢

anbcjys 2008-12-30 08:32

回复: matlab if-else条件句改写
 
[QUOTE=elohim0807;22927]matlab if-else条件句改写

function [V,a]=sphshell(r,R1,R2,rho)

r=[7000000;5000000;1500000];
G=6.672e-11;
R1=3500000;
R2=6370000;
rho=4500;

...[/QUOTE]

不用循环可以用矩阵向量计算 但是判断的如果多了 就比较麻烦了

elohim0807 2008-12-30 20:14

回复: matlab if-else条件句改写
 
能给个例子么?
实在没什么头绪..谢谢!!

remnant 2009-01-01 00:27

回复: matlab if-else条件句改写
 
用find,自己看下help.


所有时间均为北京时间。现在的时间是 13:33

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.