Calculating values in a for loop - matlab

I have a for loop that is storing values. For some reason, it is calculating values for all the number up to and including those in the for loop, instead of just the ones in the array.
t = 3600:50:172800;x = 0.1;y = 0; ro = 0.1;
T = zeros(1,length(t));
for Cm = 1E6:1E6:4E6
for i = 1:length(t)
T = T_ILS(x,y,ro,Cm,t);
Tall(Cm,:) = [T];
end
end
The error I get is "Requested 2000000x3385 array exceeds maximum array size preferences". I would like the for loop to calculate just the Cm values and not every number in between.

You are not computing every value in between, but your indices are messed up. Cm takes values in the millions, but you use it to index Tall(Cm,:). You probably want
t = 3600:50:172800;x = 0.1;y = 0; ro = 0.1;
T = zeros(1,length(t));
Cm = 1E6:1E6:4E6;
for j = 1:length(Cm)
for i = 1:length(t)
T(i) = T_ILS(x,y,ro,Cm(j),t(i));
end
Tall(j,:) = [T];
end
Notice that the main function call assigns to T(i) and uses t(i) in the function arguments to justify the existence of the for loop.

In the inner loop, T is the output of the function T_ILS, using the same arguments each time.
for i = 1:length(t)
T = T_ILS(x,y,ro,Cm,t);
Tall(Cm,:) = [T];
end
I don't know what this function computes, but you probably wanted to do this instead
for i = 1:length(t)
T = T_ILS(x,y,ro,Cm,t(i));
Tall(Cm,:) = [T];
end
to account for each value of the vector t, or even better:
for t = 3600:50:172800
T = T_ILS(x,y,ro,Cm,t);
Tall(Cm,:) = [T];
end
EDIT: Also, to make this answer complete, I'd like to merge #MadPhysicist's answer with mine. The result would be
Cm = 1E6:1E6:4E6; x = 0.1;y = 0; ro = 0.1;
T = zeros(1,length(t));
for i = 1:length(Cm)
for t = 3600:50:172800;
T = T_ILS(x,y,ro,Cm(i),t);
Tall(i,:) = [T];
end
end

Related

Incrementing array within parfor?

I am calculating sort of a histogram based on the distance between a pair of points in 3d space:
numBins = 20;
binWidth = 2.5;
pop = zeros(1,numBins);
parfor j=1:particles
r1 = coords(j,:);
for k=j+1:particles
r2 = coords(k,:);
d = norm(r1-r2);
ind = ceil(d/binWidth);
pop(ind) = pop(ind) + 1;
end
end
This, expectedly, results in
Error: The variable pop in a parfor cannot be classified.
I understand the problem, but I am confused as to how can I solve it.
In principle, what could be done is to have n copies of pop = zeroes(1,numBins) be sent to each of n workers, and joined by adding each copy together at the end of computation. How can I do this here? Or is there another, more standard way of solving the problem?
There is two things that don't work in your code:
1) for k = j+1:particles
In a parfor a nested loop should have fixed bound.
2) pop(ind)
Matlab is afraid that the for-loop order matters and display an error message. Even if, in your specific case, the order doesn't matters (But matlab is not smart enough to know that).
The solution, Linearization:
%Dummy data
numBins = 20;
binWidth = 2.5;
particles = 10;
coords = rand(10,2)*40;
%Initialization
pop = zeros(1,numBins);
parfor j=1:particles
r1 = coords(j,:)
r2 = coords((j+1):end,:)
d = sqrt(sum([r1-r2].^2,2)) % compute each norm at the same time !
pop = pop + histcounts(ceil(d/binWidth),0:numBins)
end
You can create a function that computes the inner loop and use a handle to it in the parfor (I didn't tested it but I think it should work according to the documentation):
function pop = hist_comp(pop,j,particles,coords,binWidth)
r1 = coords(j,:);
for k=j+1:particles
r2 = coords(k,:);
d = norm(r1-r2);
ind = ceil(d/binWidth);
pop(ind) = pop(ind) + 1;
end
end
numBins = 20;
binWidth = 2.5;
particles = 10;
coords = rand(10,2)*5;
pop = zeros(1,numBins);
f = #(pop,j) hist_comp(pop,j,particles,coords,binWidth);
parfor j=1:particles
pop = f(pop,j);
end

Matlab 2016b: looping with structures is much slower than with variables

I am computing a list of error statistics of a variable u. Whenever I put the structures in the loop, Matlab becomes superslow, while it is pretty fast with standard variables. I have heard that with Matlab 2016b one can either use loops or use vectorized notation, the speed should be the same. It looks like it does not work for structures. Do you know why? Best suggestion here? This is a minimal example ( I just used trivial "if" clauses, mine are more complex and that's the main reason why I don't vectorize):
n = 1000,
m = 1000;
uMODEL = rand(n,m);
uOBS = rand(n,m);
%
%
%
tic
ERRORS = struct('Emedio',0,'MAE',0,'sigma',0,'Emax',0,'Emin',0);
Nerr(1:max(n,1),1) = 0; %Nerr at most it contains n
fields = fieldnames(ERRORS);
for i = 1:numel(fields)
ERRORS.(fields{i}) = zeros(max(n,1),1);
end
for i=1:m
for j=1:n
if (1000>2 && 2000<343532)
diff = uMODEL(j,i)-uOBS(j,i);
ERRORS.Emedio(j) = ERRORS.Emedio(j) + diff;
ERRORS.MAE(j) = ERRORS.MAE(j) + abs(diff);
ERRORS.sigma(j) = ERRORS.sigma(j) + diff^2;
ERRORS.Emax(j) = max(ERRORS.Emax(j),diff);
ERRORS.Emin(j) = min(ERRORS.Emin(j),diff);
Nerr(n) = Nerr(n) + 1;
end
end
end
ERRORS.Emedio(:) = ERRORS.Emedio(:)./Nerr(:);
ERRORS.MAE(:) = ERRORS.MAE(:)./Nerr(:);
ERRORS.sigma(:) = sqrt(ERRORS.sigma(:)./(Nerr(:)-1));
toc
clear ERRORS
tic
%
% here instead I define variables, I fill them up and then I throw them in structures, cause loops with structures are strangely slow...
Emedio = zeros(max(n,1),1);
MAE = zeros(max(n,1),1);
sigma = zeros(max(n,1),1);
Nerr = zeros(max(n,1),1);
Emax = zeros(max(n,1),1);
Emin = zeros(max(n,1),1);
for i=1:m
for j=1:n
if (1000>2 && 2000<343532)
diff = uMODEL(j,i)-uOBS(j,i);
Emedio(j) = Emedio(j) + diff;
MAE(j) = MAE(j) + abs(diff);
sigma(j) = sigma(j) + diff^2;
Emax(j) = max(Emax(j),diff);
Emin(j) = min(Emin(j),diff);
Nerr(n) = Nerr(n) + 1;
end
end
end
Emedio(:) = Emedio(:)./Nerr(:);
MAE(:) = MAE(:)./Nerr(:);
sigma(:) = sqrt(sigma(:)./(Nerr(:)-1));
ERRORS.Emedio(:) = Emedio(:);
ERRORS.MAE(:) = MAE(:);
ERRORS.sigma(:) = sigma(:);
ERRORS.Emax(:) = Emax(:);
ERRORS.Emin(:) = Emin(:);
toc
the output is:
Elapsed time is 2.372765 seconds.
Elapsed time is 0.057719 seconds.

Matlab. Create a loop to change variable size with each iteration

I am currently trying to run a script that calls a particular function, but want to call the function inside a loop that halfs one of the input variables for roughly 4 iterations.
in the code below the function has been replaced for another for loop and the inputs stated above.
the for loop is running an Euler method on the function, and works fine, its just trying to run it with the repeated smaller step size im having trouble with.
any help is welcomed.
f = '3*exp(-x)-0.4*y';
xa = 0;
xb = 3;
ya = 5;
n = 2;
h=(xb-xa)/n;
x = xa:h:xb;
% h = zeros(1,4);
y = zeros(1,length(x));
F = inline(f);
y(1) = ya;
for j = 1:4
hOld = h;
hNew = hOld*0.5;
hOld = subs(y(1),'h',hNew);
for i = 1:(length(x)-1)
k1 = F(x(i),y(i));
y(i+1,j+1) = y(i) + h*k1;
end
end
disp(h)
after your comment, something like this
for j = 1:4
h=h/2;
x = xa:h:xb;
y = zeros(1,length(x));
y(1) = ya;
for i = 1:(length(x)-1)
k1 = F(x(i),y(i));
y(i+1,j+1) = y(i) + h*k1;
end
end

Index Error in for loop, MATLAB

I am running this program below and I keep getting this error "Index exceeds matrix dimensions" for line "Tour1 = pop(kk(1:10),:);". I don't know what I have missed out. Can anyone help please.
Thanks
for i = 1:100 %Population Initialization
pop(i,1) = 50 - rand*(50-1);
pop(i,2) = 1 - rand*(1-0.1);
pop(i,3) = 0.2 - rand*(0.2-0.01);
Kc(i) = pop(i,1);
T1(i) = pop(i,2);
T2(i) = pop(i,3);
fitness(i) = obj(Kc(i), T1(i), T2(i));
%% TOURNAMENT SELECTION
for j = 1:10 %Select first parent
kk = randperm(100);
Tour1 = pop(kk(1:10),:);
temp1 = mat2cell(Tour1(j,:),1,ones(1,numel(Tour1(j,:))));
ZET1(j) = obj(temp1{:});
end
[Val1 Ind1] = max(ZET1);
parent1 = Tour1(Ind1,:);
end
Just place the final end before executing the j block
for i = 1:100 %Population Initialization
pop(i,1) = 50 - rand*(50-1);
pop(i,2) = 1 - rand*(1-0.1);
pop(i,3) = 0.2 - rand*(0.2-0.01);
Kc(i) = pop(i,1);
T1(i) = pop(i,2);
T2(i) = pop(i,3);
fitness(i) = obj(Kc(i), T1(i), T2(i));
end
for j = 1:10 %Select first parent
kk = randperm(100);
Tour1 = pop(kk(1:10),:);
temp1 = mat2cell(Tour1(j,:),1,ones(1,numel(Tour1(j,:))));
ZET1(j) = obj(temp1{:});
end
[Val1 Ind1] = max(ZET1);
parent1 = Tour1(Ind1,:);
Do you really want to have the second for inside the first one?
At the first execution of the outer loop, i is one. You populate pop (1, :), but no more.
At the inner loop, you then try to access pop (kk (1:10), :), with kk (1:10) being within [1..100]. But there is no pop (i, :) for i > 1.

Handle function implicitly accounting for independent variables

I have that
clc, clear all, close all
tic
k1 = 1E-02:0.1:1E+02;
k2 = 1E-02:0.1:1E+02;
k3 = 1E-02:0.1:1E+02;
k = sqrt(k1.^2+k2.^2+k3.^2);
c = 1.476;
gamma = 3.9;
colors = {'b'};
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,numel(k));
E_int(1) = 1.5;
for i = 2:numel(k)
if k(i) < 400
E_int(i) = E_int(i-1) - integral(E,k(i-1),k(i));
elseif k(i) > 400
E_int(i) = 2.180/(k(i)^(2/3));
end %end if
end %end i
beta = (c*gamma)./(k.*sqrt(E_int));
figure
plot(k,beta,colors{1})
count = 0;
%F_11 = zeros(1,numel(k1));
F_33 = zeros(1,numel(k1));
Afterwards, I should calculate F_33 as
for i = 1:numel(k1)
count = count + 1;
phi_33 = #(k2,k3) (1.453./(4.*pi)).*(((k1(i)^2+k2.^2+(k3 + beta(i).*k1(i)).^2).^2)./((k1(i)^2+k2.^2+k3.^2).^2)).*((k1(i)^2+k2.^2)./((1+k1(i)^2+k2.^2+(k3+beta(i).*k1(i)).^2).^(17/6)));
F_33(count) = 4*integral2(phi_33,0,1000,0,1000);
end
Now let's come to my question. I know from a paper that:
k = sqrt(k1.^2+k2.^2+k3.^2);
k30 = k3 + beta.*k1;
k0 = sqrt(k1.^2+k2.^2+k30.^2);
E_k0 = 1.453.*(k0.^4./((1+k0.^2).^(17/6)));
Therefore the expression for phi_33 would result in
phi_33 = (E_k0./(4*pi.*(k.^4))).*(k1.^2+k2.^2);
The question is: how can I make use of this final expression insted of the long one I'm using at the moment (within the for loop)?
The last expression for phi_33 is easier to handle (especially because of reckless mistakes in writing the former) and it would "pass by reference" (k2,k3), which are the independent variables.
Any hint is more than welcome.
Best regards,
fpe
If I understand you correctly you want to use the new expression in exactly the same way as the old one-liner. You just want to divide your function phi33 into parts because of readability.
You could do this by placing the expression in a separate function taking all values needed for the calculation. Using your old expression exactly this would look something like this:
function phi_33 = phi_33_old(k1,k2,k3,beta,i)
phi_33 = (1.453./(4.*pi)).*(((k1(i)^2+k2.^2+(k3 + beta(i).*k1(i)).^2).^2)./((k1(i)^2+k2.^2+k3.^2).^2)).*((k1(i)^2+k2.^2)./((1+k1(i)^2+k2.^2+(k3+beta(i).*k1(i)).^2).^(17/6)));
end
This function could then be called inside your for-loop like this.
phi_33_test = #(k2,k3) phi_33_old(k1,k2,k3,beta,i);
Using the same style, a new function could be defined as follows.
function phi_33 = phi_33_new(k1,k2,k3,beta,i)
k = sqrt(k1.^2+k2.^2+k3.^2);
k30 = k3 + beta.*k1;
k0 = sqrt(k1.^2+k2.^2+k30.^2);
E_k0 = 1.453.*(k0.^4./((1+k0.^2).^(17/6)));
phi_33_allValues = (E_k0./(4*pi.*(k.^4))).*(k1.^2+k2.^2);
phi_33 = phi_33_allValues(i);
end
Note that here all values of phi_33 are calculated and then the ith value is selected. It is written in this way only to show the similarity to the old case. This new function can now be called inside the for-loop in the same way as the old one.
phi_33 = #(k2,k3) phi_33_new(k1,k2,k3,beta,i);