sorter and faster code

조회 수: 2 (최근 30일)
Nivodi
Nivodi 2018년 8월 15일
댓글: Jan 2018년 8월 16일
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
Jan
Jan 2018년 8월 15일
편집: Jan 2018년 8월 15일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2018년 8월 15일
편집: Jan 2018년 8월 15일
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
Nivodi
Nivodi 2018년 8월 16일
편집: Nivodi 2018년 8월 16일
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 2018년 8월 16일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by