Need Help in Simplifying code

조회 수: 14 (최근 30일)
Jefri Nazulrullah bin Zulkepli Amin
답변: Samatha Aleti 2019년 7월 16일
I need tips in simplifying code for the function to run faster as i am not too familiarised with the vectorisation.
Here is the code:
for j = 1:length(SurfAz)
AOI(:,j) = pvl_getaoi(SurfTilt, SurfAz(j), SunZen, SunAz);
end
TrackerAz = zeros(length(SunAz),1);
for j=1:length(SunE1)
if SunE1(j)>=0
Min = min(AOI(j,:));
idxm = AOI(j,:)==Min;
TrackerAz(j) = SurfAz(idxm);
else
TrackerAz(j) = 60;
end
end
  댓글 수: 1
dpb
dpb 2019년 7월 13일
Don't know other arguments to pvl_getaoi nor whether it is vectorized to take array inputs or not.
What does it return? As written it looks like it returns more than one value?
Did you preallocate AOI?
Need dimensions of the arrays -- there seems quite a bit of inconsistency in between what appear to be vectors versus arrays.
Also, NB: length() returns max(size(X)) for input array X. It is NOT to be relied upon to tell you the number of rows/columns in an array; use size() for the proper dimension instead to avoid surprises/errors.

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

답변 (1개)

Samatha Aleti
Samatha Aleti 2019년 7월 16일
You can use the find function for vectorization. Here is how you can simplify the code.
for j = 1:length(SurfAz)
AOI(:,j) = pvl_getaoi(SurfTilt, SurfAz(j), SunZen, SunAz);
end
TrackerAz = zeros(length(SunAz),1);
idx1 = find(SunE1>=0)
idx2 = find(SunE1<0)
for i =1:length(idx1)
[m,mIdx] = min(AOI(idx1(i),:));
TrackerAz(idx1(i)) = x(mIdx);
end
TrackerAz(idx2) = 60;

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by