Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

How to impove the performence of this code?

2 views (last 30 days)
feng
feng on 24 Nov 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
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)

Answers (1)

Hugo
Hugo on 24 Nov 2014
Edited: Hugo on 24 Nov 2014
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.

This question is closed.

Community Treasure Hunt

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

Start Hunting!