フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to impove the performence of this code?

2 ビュー (過去 30 日間)
feng
feng 2014 年 11 月 24 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello,
I am doing a for-loop, but this computing cost is too huge for me. Therefore I am trying to find a shut-cut way to reduce the cost, I have no ideas how to do this vectorization.
Any suggestions are greatly appreciated.
Many thanks
Code:
--------------------------------------------------------
felspar(197992,128);
felspar(:,1:3)=alldatafel(:,1:3);
for j = 1:197992
for i=1:337945
if strcmp(alldata(i,1),alldatafel(j,1)) && strcmp(alldata(i,2),alldatafel(j,2));
felspar(j,4:128)=alldata(i,1:125);
end
end
end
xlswrite('match.xlsx',felspar)

回答 (1 件)

Hugo
Hugo 2014 年 11 月 24 日
編集済み: Hugo 2014 年 11 月 24 日
As far as I can see, there is a problem in the code. Wheneven the condition
strcmp(alldata(i,1),alldatafel(j,1)) && strcmp(alldata(i,2),alldatafel(j,2))
is TRUE, then
felspar(j,4:128)=alldata(i,1:125)
But that means that the value stored in felspar(j,4:128) for each j corresponds to the largest value of i for which the condition is fulfilled. Therefore, you can save time by searching i from the end, and breaking the loop as soon as the condition is fulfilled. The could should look like this:
felspar(197992,128);
felspar(:,1:3)=alldatafel(:,1:3);
for j = 1:197992
for i=337945:-1:1
if strcmp(alldata(i,1),alldatafel(j,1)) && strcmp(alldata(i,2),alldatafel(j,2));
felspar(j,4:128)=alldata(i,1:125);
break;
end
end
end
xlswrite('match.xlsx',felspar)
You could also save time by preserving in alldata and alldatafel only those items that are unique, using the unique function.
Hope this helps.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by