Filter löschen
Filter löschen

this program taking a long time to run and i did't get the answer also due to this problem. how can i reduce this time

2 Ansichten (letzte 30 Tage)
my data contain 1182614X3 datas, s=[]; x=data(1:end,1); y=data(1:end,2); z=data(1:end,3); x1=5; x2=9; y1=10; y2=14; z1=25; data2=[x1 y1 z1]; mainslope=(y2-y1)/(x2-x1); count=length(x); for i=1:count x3=x(i); y3=y(i); slope=(y3-y1)/(x3-x1); s=[s;x3 y3 slope]; if slope==mainslope z3=z(i); data2=[data2; x3 y3 z3]; end end

Antworten (1)

Jos (10584)
Jos (10584) am 17 Feb. 2014
The slowness is caused by the fact to you expand the matrix s and sometimes the matrix data2 in each iteration. You should preallocated these. I also removed the if-end construction as you can easily weed out the unwanted values of data2 afterwards.
x=data(1:end,1); y=data(1:end,2); z=data(1:end,3);
x1=5; x2=9; y1=10; y2=14; z1=25;
mainslope=(y2-y1)/(x2-x1);
count=length(x);
% pre-allocation
s = nan(count,3) ;
data2 = nan(count+1,3) ; % apparently, data2 can become 1 longer than s ...
data2(1,:) = [x1 y1 z1];
for i=1:count
x3=x(i); y3=y(i);
slope=(y3-y1)/(x3-x1);
s(i,:) = [x3 y3 slope] ;
data2(i+1,:) = [x3 y3 z(i)] ;
end
TF = s(:,3) == mainslope ; % OR abs(s(:,3)-mainslope) < 0.001 ???
data2 = data2([true ; TF],:) ; % only retain when slope equals mainslope (plus the first row)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by