sorter and faster code

1 view (last 30 days)
Nivodi
Nivodi on 15 Aug 2018
Commented: Jan on 16 Aug 2018
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 Comment
Jan
Jan on 15 Aug 2018
Edited: Jan on 15 Aug 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.

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Aug 2018
Edited: Jan on 15 Aug 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 Comments
Nivodi
Nivodi on 16 Aug 2018
Edited: Nivodi on 16 Aug 2018
Thank you very much Jan, but I didn't get exactly what I wanted. I modified the pre-allocation so I took the table with the new concentrations (without the outliers) but in the second column the livetime wasn't the corresponding (each concentration has its own livetime and it shouldn't be messed)
Jan
Jan on 16 Aug 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!