Help vectorizing code to improve speed
이전 댓글 표시
I have a function (below) that will be called many times. The profiler reveals that the slowest line in the function (by a lot) is:
G = [x(mask,i),y(mask,i),z(mask,i),ones(nnz(mask),1)];
I was surprised since this is simply assembling the G matrix. The matrix operations (rcond, and "\") are much faster. I tried bulding the G matrix outside of the for loop, but it is three dimensional, and I had to do a squeeze() on the matrix slices inside the loop, which was even slower.
Can anyone help vectorize this code more than it is already or help build the G matrix more efficiently?
Any other efficiency tips are appreciated.
Thank you!
function dop = get_dop(az, el, sat_mask, minel)
%Calulate Pdop using a vector a az/el values from visible satellites to a single ground point.
%Input:
% az: vector of azimuths for each satellite (row) at each time (column)
% el: vecotr of elevations for each satellite (row) at each time (column)
% sat_mask: vector of rows of az/el to include
% min_el: minimum elevation angle to include satellite row in calculation
%Output:
% pdop: vector of pdop values corresponding to az/el values for the given ground point
%
sat_mask = boolean(sat_mask);
az = squeeze(az(:,sat_mask,:));
el = squeeze(el(:,sat_mask,:));
el_mask = el >= minel;
az = deg2rad(az);
el = deg2rad(el);
[x,y,z] = sph2cart(az,el, ones(size(az)));
dop = nan(1,size(el,2));
for i = 1:size(el,2)
%Elevation mask to get when the sats are actually in view
mask = el_mask(:,i);
if nnz(mask) >= 3 %Ensure that at least 3 satellites are available before proceeding
G = [x(mask,i),y(mask,i),z(mask,i),ones(nnz(mask),1)]; %matrix of all the x,y,z coordinates for in view satellites
if rcond(G'*G) > 1e-8 %Rcond check to ensure matrix is invertible. If not, skip and dop remains NaN.
P = (G'*G)\eye(4);
dop(i) = sqrt(P(1,1)+P(2,2)+P(3,3));
end
end
end
end
댓글 수: 5
the cyclist
2023년 8월 30일
That line is not just assembling G; it is also all the indexing and matrix creation that that line contains.
Can you give an example of the parameters used to call that function, so that we don't have to guess?
Michael Jones
2023년 8월 30일
Michael Jones
2023년 8월 30일
KSSV
2023년 8월 30일
You should initilaze the variables which you are filling in the loop. What is the function wrapTo360?
Michael Jones
2023년 8월 30일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 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!

