Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

# How to impove the performence of this code?

1 visualizzazione (ultimi 30 giorni)
feng il 24 Nov 2014
Chiuso: MATLAB Answer Bot il 20 Ago 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)

### Risposte (1)

Hugo il 24 Nov 2014
Modificato: Hugo il 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.

### Community Treasure Hunt

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

Start Hunting!

Translated by