From: Luna Moon on
How to make my code faster? Hate my stupid coding!

Hi all,

Here is my code: here the two matrices are Nx2 dTmp1 and dTmp2.

dTmp1(:, 1) and dTmp2(:, 1) are datestamps.

dTmp1(:, 2) and dTmp2(:, 2) are data.

Basically I want to do the following:

if someone give me a set, either the intersection or union or other
operations of dTmp1(:, 1) and dTmp2(:, 1).

I would like to use that as indices and to get get the corresponding
data points from dTmp1(:,2) and dTmp2(:, 2).

I would liek the fastest appraoch...

Thanks
------------------------

nDates=intersect(dTmp1(:, 1), dTmp2(:, 1));

nDates=nDates(:);

dData1=zeros(size(nDates));
dData2=zeros(size(nDates));

for j=1:length(nDates)

nIdx=find(nDates(j)==dTmp1(:, 1));

dData1(j)=dTmp1(nIdx, 2);

nIdx=find(nDates(j)==dTmp2(:, 1));

dData2(j)=dTmp2(nIdx, 2);

end;
From: dpb on
Luna Moon wrote:
....

> if someone give me a set, either the intersection or union or other
> operations of dTmp1(:, 1) and dTmp2(:, 1).
>
> I would like to use that as indices and to get get the corresponding
> data points from dTmp1(:,2) and dTmp2(:, 2).
>
> I would liek the fastest appraoch...
>
> Thanks
> ------------------------
>
> nDates=intersect(dTmp1(:, 1), dTmp2(:, 1));
> nDates=nDates(:);
> dData1=zeros(size(nDates));
> dData2=zeros(size(nDates));
> for j=1:length(nDates)
> nIdx=find(nDates(j)==dTmp1(:, 1));
> dData1(j)=dTmp1(nIdx, 2);
> nIdx=find(nDates(j)==dTmp2(:, 1));
> dData2(j)=dTmp2(nIdx, 2);
> end;


[nDates,iA,iB]=intersect(dTmp1(:, 1), dTmp2(:, 1));
dData1=dTmp1(iA, 2);
dData2=dTmp2(iB, 2);

doc intersect % note optional outputs

Note that in general all the similar functions have same feature...

--