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