How to improve the code for vector calculation

2 Ansichten (letzte 30 Tage)
krishnamurthy
krishnamurthy am 6 Jun. 2013
N=2;
MM=4;
AA=rand(1,12);AA1=AA(1:3);AA2=AA(4:6);AA3=AA(7:9);AA4=AA(10:12);
BB=rand(1,12);BB1=BB(1:3);BB2=BB(4:6);BB3=BB(7:9);BB4=BB(10:12);
DD=rand(1,12);DD1=DD(1:3);DD2=DD(4:6);DD3=DD(7:9);DD4=DD(10:12);
EE=rand(1,12);EE1=EE(1:3);EE2=EE(4:6);EE3=EE(7:9);EE4=EE(10:12);
HH=rand(1,12);HH1=HH(1:3);HH2=HH(4:6);HH3=HH(7:9);HH4=HH(10:12);
BBB=rand(12,3);BBB1=BBB((1:3),:);BBB2=BBB((4:6),:);BBB3=BBB((7:9),:);BBB4=BBB((10:12),:);
LAD=rand(1,4);LAD1=LAD(1);LAD2=LAD(2);LAD3=LAD(3);LAD4=LAD(4)
for iter=1:N
iter
for i=1:MM
if i==1
AA=AA1;BB=BB1;DD=DD1;EE=EE1;HH=HH1;BBB=BBB1;LAD=LAD1;
elseif i==2
AA=AA2;BB=BB2;DD=DD2;EE=EE2;HH=HH2;BBB=BBB2;LAD=LAD2;
elseif i==3
AA=AA3;BB=BB3;DD=DD3;EE=EE3;HH=HH3;BBB=BBB3;LAD=LAD3;
elseif i==4
AA=AA4;BB=BB4;DD=DD4;EE=EE4;HH=HH4;BBB=BBB4;LAD=LAD4;
end
Eeta=diag((AA+HH.*DD)./LAD)+BBB;
Delta=0.5*(1-(BB+HH.*EE)./LAD)';
Penta=Eeta\Delta;
Penta1(i,:)=Penta
end
PPP=reshape(Penta1',1,12)
end

Antworten (1)

Jan
Jan am 6 Jun. 2013
It is a bad idea to pack indices into the name of the variables like in "AA4". If you use a cell instead, the FOR-loop get's much nicer:
AA = rand(1,12);
AAC {AA(1:3), AA(4:6), AA(7:9), AA(10:12)};
...
for i = 1:MM
AA = AAC{i};
...
I do not assume that a completely vectorized version is nicer or faster.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by