필터 지우기
필터 지우기

How to do it more efficiently?

조회 수: 1 (최근 30일)
Ilham Hardy
Ilham Hardy 2014년 12월 23일
댓글: Ilham Hardy 2014년 12월 24일
Hi,
In my script, this piece of code is taking about 70% of processing time.
The idea is to create uniform timestamp, it searches the time array and compare it with the Tsynced. if there is no similar entry, the other parameters to NaN.
Is there anyway to change the script below without using find function? Even better if no for-loop is required.
Code:
for ix = 1:length(time_synced)
[r,~,~] = find(cCells(:,1)==time_synced(ix),1,'first');
if isempty(r)
curArr(ix,:) = NaN;
else
curArr(ix,:) = cCells(r,:);
end
end
Thanks.

채택된 답변

Roger Stafford
Roger Stafford 2014년 12월 23일
See if this is faster:
[t,ic] = ismember(time_synced,cCells(:,1));
curArr(t) = cCells(ic);
curArr(~t) = NaN;
Note: This assumes that 'time_synced' and 'curArr' are vectors of the same length.
  댓글 수: 2
Ilham Hardy
Ilham Hardy 2014년 12월 23일
Ah, yes. It is way faster than the previous find+for-loop method.
Thanks.
Ilham Hardy
Ilham Hardy 2014년 12월 24일
A bit correction for other fellow readers,
[t,ic] = ismember(time_synced,cCells(:,1));
curArr(t,:) = cCells(ic,:);
curArr(~t,:) = NaN;
PS: The curArr is preallocated.

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2014년 12월 23일
[l0,ii] = ismember(time_synced,cCells(:,1));
out = nan(numel(time_synced),size(cCells,2));
out(l0,:) = cCells(ii(l0),:);
  댓글 수: 1
Ilham Hardy
Ilham Hardy 2014년 12월 24일
Many thanks for the answer,

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by