Hi,
Is it possible to convert the loop below into a vector calculation? Thank you.
dataset=table();
dataset.value=rand(30,1);
dataset.time=sort(rand(30,1));
timefilter=dataset.time-0.2;
result1=repmat(NaN,height(dataset),1);
result2=repmat(NaN,height(dataset),1);
result3=repmat(NaN,height(dataset),1);
%Loop:
for row=5:height(dataset)
startrow=min(find(dataset.time >=timefilter(row)));
result1(row,1)=nansum(dataset.value(startrow:row));
result2(row,1)=length(dataset.value(startrow:row));
result3(row,1)=nanstd(dataset.value(startrow:row)) ;
end

댓글 수: 3

Roger Stafford
Roger Stafford 2018년 2월 24일
편집: Roger Stafford 2018년 2월 24일
|I would say that the chance of finding a reasonable vectorization of your for-loop is very slim. The trouble is that 'startrow' is a quite unpredictable quantity dependent on the vagaries of the random ascending values in 'dataset.time' and is not easily calculated without some kind of for-loop. It is the index of the first 'dataset.time' value whose difference from its value at index 'row' is less than or equal to .2 as 'row' ascends and that is difficult to vectorize. I believe it would be best done as an iterative process where one step depends on the results of the previous step, and that is not really the nature of most vectorization schemes. I think there exists a possibly better iterative method than is involved in your for-loops with their inefficient 'find' operation, however it would require a bit of time working it out.
I would point out that 'nansum' and 'nanstd' are surely unnecessary operations as opposed to 'sum' and 'std' since 'dataset.value' contains no NaN values. |
Azura Hashim
Azura Hashim 2018년 2월 26일
Thanks. Yes nansum and nanstd is not required here. I took it from the code that handles the full size data which has NaNs.
Meade
Meade 2018년 2월 26일
The sum, length, and std calculations are so trivially fast that it might be fastest to calculate them for the entire array, then just sort. If this doesn't seem unreasonable for your use, I would use bsxfun or arrayfun on your data, then sort. That would be fastest I think.
That would get rid of the loop entirely.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 2월 24일

댓글:

2018년 2월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by