Need Help in Simplifying code

1 view (last 30 days)
Jefri Nazulrullah bin Zulkepli Amin
Answered: Samatha Aleti on 16 Jul 2019
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 Comment
dpb
dpb on 13 Jul 2019
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.

Sign in to comment.

Answers (1)

Samatha Aleti
Samatha Aleti on 16 Jul 2019
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;

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!