sorter and faster code
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello everyone, I am quite new in Matlab and I need your help. Can someone tell me how can I make this code shorter and (maybe) faster? I have to repeat the code for several elements. Thank you!
load ('TableScatter_B6.mat')
TableScatter_B6.LIVETIME= cellfun(@str2num,TableScatter_B6.LIVETIME);
array=table2array(TableScatter_B6);
x=array(:,13);%LIVETIME
Rb=array(:,3);Sr=array(:,4);Y=array(:,5);Zr=array(:,6);Nb=array(:,7);Mo=array(:,8);I=array(:,9);
Cs=array(:,10);Ba=array(:,11);U=array(:,12);LIVETIME=array(:,13);
meanValue_Sr = mean(Sr);
absoluteDeviation_Sr = abs(Sr - meanValue_Sr);
mad_Sr = median(absoluteDeviation_Sr);
sensitivityFactor = 6 ;
thresholdValue_Sr = sensitivityFactor * mad_Sr;
outlierIndexes_Sr = abs(absoluteDeviation_Sr) > thresholdValue_Sr;
outliers_Sr = Sr(outlierIndexes_Sr);
nonOutliers_Sr = Sr(~outlierIndexes_Sr);
T_V_Sr=TableScatter_B6(:,[4,13])
for w=1:length(Sr);
for j=1:length(nonOutliers_Sr);
if nonOutliers_Sr(j)==Sr(w,1);
Tnew_Sr(j,:)=T_V_Sr(w,:) ;
end
end
end
1 comentario
Jan
el 15 de Ag. de 2018
Editada: Jan
el 15 de Ag. de 2018
Today I've formatted the code for you. Please use the "{} Code" button by your own in the future - thanks.
You forgot to explain the purpose of the code. In the nested loop, Tnew_Sr(j,:) can be overwritten repeatedly by different T_V_Sr(w,:) this is surely a waste of time, but it is not clear, if this wanted or a bug.
Respuesta aceptada
Jan
el 15 de Ag. de 2018
Editada: Jan
el 15 de Ag. de 2018
I assume the nested loop is the bottleneck of your code. Use the profile to check this. Remember that it is not useful to improve the runtime of a line, which needs only 2% of the total time. If you accelerate it by a factor 2, the total time is reduced by 1% only.
Start with writing one command per line. Decreasing the size of the code is not useful and has no relation to an improvement of the speed. With one command per line, Matlab's JIT accelerator can evaluate the lines in a different order to increase the speed.
Stay away from loading variables directly into the workspace, because this can impede the JIT massively.
% load ('TableScatter_B6.mat') Better:
data = load ('TableScatter_B6.mat');
Then care for a proper pre-allocation before the loop:
Tnew_Sr = zeros(length(Sr), size(T_V_Sr, 2));
Now measure the time again to have a fair comparison.
Finally try to replace the loops by:
[match, index] = ismember(nonOutliers_Sr, Sr);
Tnew_Sr(match, :) = T_V_Sr(index, :);
Does this give you the wanted output? Maybe you want a 'last' flag in ismember.
Another hint: Prefer str2double(c) instead of cellfun(@str2num, c). But I do not assume, that this is critical here.
2 comentarios
Jan
el 16 de Ag. de 2018
@DImi Zerv: I don't get it. Does the ismember approach a different result than you two loops? Please post your current code, because explanations like "concentrations", "outliers" and "livetime" are meaningful only for an insider.
Más respuestas (0)
Ver también
Categorías
Más información sobre Performance and Memory en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!