MATLAB: Simple nested loops error

nested loops

Hi,
I want to do modfication(nested loop) in the current code limts[r,c].
I have twelve files avg_mat(i = 12) to Process . But each file needs to use diffrent r and c values in the code. I need to mutiply r and c with cosine theta for every file.
where theta increases from 0deg to max deg and decreases to 0deg at the end with the increment (max/number of files). Please help.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
results = zeros(numl(avg_mat),1);
for i=1:numel(avg_mat)
writematrix(avg_mat{i}, ['avg_val_' num2str(i)]);
R = avg_mat{i}(:,1);
C = avg_mat{i}(:,2);
F = avg_mat{i}(:,5);
maxi = 90;
for t = [0:(maxi/numl(avg_mat)):maxi:-(maxi/numl(avg_mat)):0]
r(t) = linspace(min(R), max(R), 1000)*cos(t);%need modification based the file
c(t) = linspace(min(C), max(C), 1000)*cos(t);%need modification based on the file
[Rg, Cg] = meshgrid(r(t), c(t));
Fg(i,t) = griddata(R, C, F, Rg, Cg);
result(i,t) = trapz(c(t), trapz(r(t), Fg, 2));
end
end

Best Answer

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
navg = numel(avg_mat);
r = cell(navg,1);
t = cell(navg,1);
maxi = 90;
t1 = linspace(0, maxi, navg);
t2 = fliplr(t1(1:end-1));
tvals = [t1, t2];
nt = length(tvals);
results = zeros(navg,nt);
Fg = cell(navg,nt);
for i = 1:navg
writematrix(avg_mat{i}, ['avg_val_' num2str(i)]);
R = avg_mat{i}(:,1);
C = avg_mat{i}(:,2);
F = avg_mat{i}(:,5);
for tidx = 1:nt
t = tvals(tidx);
r{t} = linspace(min(R), max(R), 1000)*cosd(t);
c{t} = linspace(min(C), max(C), 1000)*cosd(t);
[Rg, Cg] = meshgrid(r, c);
Fg{i,tidx} = griddata(R, C, F, Rg, Cg);
result(i,t) = trapz(c{t}, trapz(r{t}, Fg{i,tidx}, 2));
end
end
I suspect that you do not really need to record all those r and c and Fg values, but recording them is at least useful during the debugging phase.