Filter löschen
Filter löschen

Help optimizing/vectorizing a code in which loops are used to fill several vectors

1 Ansicht (letzte 30 Tage)
Hello,
I'm trying to fill in several vectors from the info that I have on a text file. I load the different columns of that file o severall Arrays A, B, C, D, E, F, and G.
The size of my vectors is quite big, (10^6 or 10^7) elements so the code I show next takes a long time to run.
Can someone help me with a vectorization of this in order to make it faster?
[A,B,C,D,E,F,G] = textread(strcat(PathName,FileName),'%f %d %d %d %d %s %s');
n=size(A,1);
for i=1:n
if C(i) == 0 && D(i) == 0
prim(i)=A(i);
elseif C(i)==0 && D(i) ~= 0
sc(i)=A(i);
elseif C(i)~=0 && C(i) ==0
sc2(i)=A(i);
end
if C(i)+D(i)==1
o1(i)=A(i);
elseif C(i)+D(i)==2
o2(i)=A(i);
elseif C(i)+D(i)==3
o3(i)=A(i);
elseif C(i)+D(i)==4
o4(i)=A(i);
elseif C(i)+D(i)>4
osup(i)=A(i);
end
end
  1 Kommentar
Kevin Holst
Kevin Holst am 31 Jul. 2012
I haven't looked into your code too much but I believe this line is incorrect anyways:
elseif C(i)~=0 && C(i) ==0
shouldn't it be:
elseif C(i)~=0 && D(i) ==0

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John Petersen
John Petersen am 31 Jul. 2012
Here's one way to do it
sc = [];
i = (~C & D);
sc(i) = A(i);
o3 = zeros(n,1);
CpD = C + D;
i3 = find(CpD ==3);
i4 = find(CpD ==4);
o3(i3) = A(i3);
o4(i4) = A(i4);
Follow similar logic for the rest of your code.

Weitere Antworten (1)

carlos Uribe
carlos Uribe am 1 Aug. 2012
Thanks very much for taking a look at this...worked like a charm

Kategorien

Mehr zu Entering Commands 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