How to quickly find the first NaN?

6 ビュー (過去 30 日間)
Serbring
Serbring 2019 年 10 月 14 日
編集済み: Walter Roberson 2019 年 10 月 14 日
Hi all,
I am converting more than 100GB of .ASC CAN BUS data. The algorithm is pretty slow due to the fact there are struct data inside a struct data. Below you will find a basic example of my code. The line that makes it pretty slow is the following because it is looped more than a million of times.
Pos2Paste=find(isnan(Sigs{iSig2Load(j)}),1);
Is there a faster way to find the first NaN element where I can assign the value found in the struct? Maybe, I can speed up the code in another way?
Thank you in advance.
Best regards
SigNames={'s1','s2','s3','s4','s5'; 'a1','a1','a1','a2','a2'};
MessLength=40;
for i=1:2:MessLength
messageStruct(i).Name='a1';
messageStruct(i).Signals.s1=1;
messageStruct(i).Signals.s2=2;
messageStruct(i).Signals.s3=3;
end
for i=2:2:MessLength
messageStruct(i).Name='a2';
messageStruct(i).Signals.s4=4;
messageStruct(i).Signals.s5=5;
end
UniqueMessFound=unique({messageStruct.Name});
UniqueMessageCount=countcats(categorical({messageStruct.Name},UniqueMessFound));
for iSig=1:length(SigNames(1,:))
Sigs{iSig}=NaN(UniqueMessageCount(strcmp(UniqueMessFound,SigNames{2,iSig})),1);
end
for i=1:MessLength
Sig=fieldnames(messageStruct(i).Signals);
[~,iSig2Load , iSig]=intersect(SigNames(1,:),Sig);
Sig2Sel=SigNames(1,iSig2Load);
for j=1:length(Sig2Sel)
Pos2Paste=find(isnan(Sigs{iSig2Load(j)}),1);
Sigs{iSig2Load(j)}(Pos2Paste)=single(messageStruct(i).Signals.(Sig{iSig(j)}));
end
end

採用された回答

Walter Roberson
Walter Roberson 2019 年 10 月 14 日
編集済み: Walter Roberson 2019 年 10 月 14 日
You could adapt https://www.mathworks.com/matlabcentral/fileexchange/24641-vectorized-find-with-first-option to do the nan testing.

その他の回答 (1 件)

Mir Amid Hashemi
Mir Amid Hashemi 2019 年 10 月 14 日
編集済み: Mir Amid Hashemi 2019 年 10 月 14 日
Hi,
I put MessLength to 40000. I would think the bigger worry is line 23 with intersection. Isn't it?
If you are using it to find signals in a few SigNames can't you make a faster line there?
I'm sorry I'm dodging the question maybe.
  1 件のコメント
Serbring
Serbring 2019 年 10 月 14 日
Thanks Mir Amid for your answer. Actually, I am not using the intersect function embedded function, but a custom one that avoid array sorting. So, the bottle neck was the intersect function, but now, it is that line.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by