Effacer les filtres
Effacer les filtres

How to do it more efficiently?

1 vue (au cours des 30 derniers jours)
Ilham Hardy
Ilham Hardy le 23 Déc 2014
Commenté : Ilham Hardy le 24 Déc 2014
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.

Réponse acceptée

Roger Stafford
Roger Stafford le 23 Déc 2014
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 commentaires
Ilham Hardy
Ilham Hardy le 23 Déc 2014
Ah, yes. It is way faster than the previous find+for-loop method.
Thanks.
Ilham Hardy
Ilham Hardy le 24 Déc 2014
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 23 Déc 2014
[l0,ii] = ismember(time_synced,cCells(:,1));
out = nan(numel(time_synced),size(cCells,2));
out(l0,:) = cCells(ii(l0),:);
  1 commentaire
Ilham Hardy
Ilham Hardy le 24 Déc 2014
Many thanks for the answer,

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by