Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
How to impove the performence of this code?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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)
0 comentarios
Respuestas (1)
Hugo
el 24 de Nov. de 2014
Editada: Hugo
el 24 de Nov. de 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.
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!