How to instead the "for" loop?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello, I had a program which use for loop and if, but it take a long time, I want it faster.
How Can I instead of "for" and "if" so that it can make the program become faster?
Here is the program, Thanks
tic
clear all
close all
mov = mmreader('02.avi');
for i = 1: 7111 % number of the frame of the video
frame1 = read(mov, i);
if i == 750
for a = 300 :2: 303;
for b = 353 :2: 381;
for c = 301 :2: 303;
for d = 354 :2: 381;
frame1(a,b,:) = [0,0,0];
frame1(c,d,:) = [0,0,0];
end
end
end
end
for a = 300 :2: 303;
for b = 354 :2: 381;
for c = 301 :2: 303;
for d = 353 :2: 381;
frame1(a,b,:) = [153,153,153];
frame1(c,d,:) = [153,153,153];
end
end
end
end
end
if i == 760
for a = 300 :2: 303;
for b = 356 :2: 385;
for c = 301 :2: 303;
for d = 357 :2: 385;
frame1(a,b,:) = [153,153,153];
frame1(c,d,:) = [153,153,153];
end
end
end
end
for a = 300 :2: 303;
for b = 357 :2: 385;
for c = 301 :2: 303;
for d = 356 :2: 385;
frame1(a,b,:) = [0,0,0];
frame1(c,d,:) = [0,0,0];
end
end
end
end
end
for x = 1 : 478;
for y = 1 : 640;
if frame1(x,y,1) == frame1(x,y,2) && frame1(x,y,2) == frame1(x,y,3) && frame1(x+1,y,1) == frame1(x+1,y,2) && frame1(x+1,y,2) == frame1(x+1,y,3) && frame1(x+2,y,1) == frame1(x+2,y,2) && frame1(x+2,y,2) == frame1(x+2,y,3)
figure; imagesc(frame1);
title(['Frame ', num2str(i)])
xlabel('x-axis');
ylabel('y-axis');
coordinate = [x y]
frame1 = read(mov, i+1);
end
end
end
end
toc
Thanks
댓글 수: 0
답변 (1개)
Richard Brown
2012년 4월 24일
Un-nest your loops - your a,b and c,d loops are independent of each other. What you're doing is running your inner two loops gazillions of times more than you need to. I.e. replace your four-level loops with these:
for a = ...
for b = ...
frame1(a, b, :) =...
end
end
for c = ...
for d = ...
frame1(c, d, :) =...
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!