필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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
Kevin Holst
Kevin Holst 2012년 7월 31일
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

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

채택된 답변

John Petersen
John Petersen 2012년 7월 31일
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.

추가 답변 (1개)

carlos Uribe
carlos Uribe 2012년 8월 1일
Thanks very much for taking a look at this...worked like a charm

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by