Need Help in Simplifying code

1 回表示 (過去 30 日間)
Jefri Nazulrullah bin Zulkepli Amin
Jefri Nazulrullah bin Zulkepli Amin 2019 年 7 月 13 日
回答済み: 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 Center および File ExchangeVisual Exploration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by