필터 지우기
필터 지우기

How to optimize this for loop?

조회 수: 1 (최근 30일)
Prakash Choudhary
Prakash Choudhary 2019년 5월 10일
답변: Bob Thompson 2019년 5월 10일
% % % % --------------------
for i=1:size(v0)
if (v0(i)<=2)
v0(i) = 0;
else
if (v0(i) >= 12 && v0(i) <= 25)
v0(i) = 12;
else
if (v0(i) >= 26)
v0(i) = 0;
end
end
end
end
% condition for another wind speed
for i=1:size(v0)
if (v1(i)<=2)
v1(i) = 0;
else
if (v1(i) >= 12 && v1(i) <= 25)
v1(i) = 12;
else
if (v1(i) >= 26)
v1(i) = 0;
end
end
end
end
% conditon for another wind didrection
for i=1:size(v0)
if (v2(i)<=2)
v2(i) = 0;
else
if (v2(i) >= 12 && v2(i) <= 25)
v2(i) = 12;
else
if (v2(i) >= 26)
v2(i) = 0;
end
end
end
end

답변 (1개)

Bob Thompson
Bob Thompson 2019년 5월 10일
v0(v0<=2) = 0;
v0(v0>=26) = 0;
v0(v0>=120) = 12;
Should work for all three, because they're apparently the same with different labels? If they're all the same size I would strongly suggest avoiding using dynamically named variables. You can just concatenate them into a single matrix with another dimension. For example, if they are a single dimension, then they combine to a 2D matrix that you can loop through the second dimension on (not that you have to because you're looking for the same conditions and outputs. Then you only have to use the above code once, instead of three times.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by