필터 지우기
필터 지우기

Any ideas how vectorise this?

조회 수: 1 (최근 30일)
Harry Smith
Harry Smith 2017년 11월 10일
댓글: Harry Smith 2017년 11월 10일
Playing around with stock selection algorithm, hit this thousands of times any idea how to vectorise this. Any help appreciated.
function distances = calDistances(stocks, geneVector, dayN)
defaultNoData = 100;
distances(length(stocks)) = 100;
% Calculate Distances
for stockN = 1 : length(stocks)
% Get Stock Vector
stockVector = stocks(stockN).getStockVector(dayN);
% No Stock Data Check
if not(isnan(stockVector))
% Euclidian Distance
distances(stockN) = norm(stockVector - geneVector);
else
% Default no data
distances(stockN) = defaultNoData;
end
end
  댓글 수: 2
Jan
Jan 2017년 11월 10일
What is the purpose of "not(isnan(stockVector))"? Do you want:
if all(not(isnan(stockVector)))
?
Harry Smith
Harry Smith 2017년 11월 10일
Not all stocks have full data therefore to prevent an error in the euclidian distance I only use stocks with full data at that on that day, else default value

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

채택된 답변

Jan
Jan 2017년 11월 10일
편집: Jan 2017년 11월 10일
At first a simplified version:
function distances = calDistances(stocks, geneVector, dayN)
defaultNoData = 100;
distances = repmat(defaultNoData, 1, length(stocks));
% Calculate Distances
for stockN = 1 : length(stocks)
% Get Stock Vector
stockVector = stocks(stockN).getStockVector(dayN);
% No Stock Data Check
if all(isfinite(stockVector))
% Euclidian Distance
distances(stockN) = norm(stockVector - geneVector);
end
end
Use the profiler to check, if calling norm() uses a significant part of the time. If so, replace it by:
distances(stockN) = sqrt(sum((stockVector - geneVector).^2));
While calculating the norm could be vectorized, this line cannot:
stockVector = stocks(stockN).getStockVector(dayN);
What is the class of stocks(stockN).getStockVector? Does getStockVector(dayN) really reply a vector?
Using a nested struct prevents a vectorization. You have to decide if the representation of the data should be nice to read or efficient to process.
  댓글 수: 1
Harry Smith
Harry Smith 2017년 11월 10일
Thanks for the comment on euclidian distance, I'll have a look at that. Ive just checked the getStockVector, absolutely fine
The algorithm works fine just slow (24hours in a GA)

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by