just started with matlab, and vectorization, FF to problem:
What to do:
%n,t are vectors(1D arrays) EDIT: these are column vectors.
k=9;
i=1;
kv = 0.6*k:0.2*k:1.4*k;
[zs,zb] = size(k);
error1 = zeros(zs,1);
for k2 = kv
error1(i,1) = error_km(n,t,kv(i));
i= i+1;
end
where error_km is : (n and t are of same size)
function [ returnV] = error_km( n,t,k )
returnV = 0
[a,b] = size(n);
Nt = zeros();
for i=2:a
Nt(i,1) = (n(i-1,1)^-1 + k*(t(i,1)-t(i-1,1)))^-1;
error1 = Nt(i,1) - n(i,1);
returnV = returnV + error1*error1;
end
The whole program takes about 3 to 4 minutes, for small test cases, i was able to make and understand simple vectorized alternatives for loops, but i am not able to make sense to vectorize this loop,
Any guidance?
UPDATE : in error_km, now i am using:
Nt = (n(1:a-1,1).^-1 + (diff(t(:,1))).*k).^-1 - n(2:a,1);
Nt = Nt.^2;
returnV = sum(Nt);
works good, but still in the main program i am forced to use a loop for iterating over kv, if i use kv(:) it passes the vector to function not a single value each time.
used this in main : error1 = error_km(n,t,kv.*1);
is it possible to get rid of main loop also?
EDIT2, Soln :
To get rid of main loop just use arrayfun
error_km
does not use Nt you could get away with not storing all values ofNt
generated useNt=...
instead of Nt(i,1). Or preallocateNt
by changingNt=zeros();
to Nt=zeros(a,1)`. I don't have an answer about vectorizing the code at this time. - Azim J