Borrar filtros
Borrar filtros

simple nested loops error

2 visualizaciones (últimos 30 días)
MS
MS el 27 de Abr. de 2020
Comentada: MS el 5 de Mayo de 2020
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Abr. de 2020
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
  8 comentarios
MS
MS el 28 de Abr. de 2020
Editada: MS el 28 de Abr. de 2020
sorry, it was a typo. it should be numel. can you please Check the error in the results output. It has 23 columns. The first and last column are the results with the same values, remaining columns are NAN values.
Walter Roberson
Walter Roberson el 28 de Abr. de 2020
Your original posted code had
result(i,t) = trapz(c(t), trapz(r(t), Fg, 2));
That was apparently intended to produce one result for every element of avg_mat and every t value. So that is what my code does.

Iniciar sesión para comentar.

Más respuestas (1)

MS
MS el 28 de Abr. de 2020
Editada: Walter Roberson el 28 de Abr. de 2020
I am attaching you the 12 input files for your kind refernce. Every file should give one result(1*1) result. I should get only result as 12 *1 for the 12 files. did i do something wrong? can you please check.
earlier code was
result(i) = trapz(c, trapz(r, Fg, 2));
it gave me 1*1 result for every file.
we are mutiplying the limts[Rg, Cg] by cos (t). it should give the results 1*1 for every file. I might have confused you.
now, it converted to
result(i,t) = trapz(c(t), trapz(r(t), Fg, 2));
can you please check.
  32 comentarios
Walter Roberson
Walter Roberson el 4 de Mayo de 2020
navg = numel(avg_mat);
r = cell(navg,1);
t = cell(navg,1);
maxi = 90;
t1 = linspace(15, maxi, 6);
t2 = fliplr(t1(1:end));
tvals = [t1, t2];
nt = length(tvals);
results = zeros(navg,1);
Rg = cell(navg,1);
Cg = cell(navg,1);
Fg = cell(navg,nt);
filenames = cell(navg,1);
for i = 1:navg
filenames{i} = sprintf('avg_val_%d.txt', i);
writematrix(avg_mat{i}, filenames{i});
R = avg_mat{i}(:,1);
C = avg_mat{i}(:,2);
F = avg_mat{i}(:,5);
r0 = linspace(min(R), 0.1, 1000);
c0 = linspace(min(C), 0.1, 1000);
[Rg0, Cg0] = meshgrid(r0, c0);
Xc = .05;%centroid fo the roation
Yc = .05;
%assuming center of rotation is (Xc, Yc) and angle th is degrees
th = tvals(i);
m = makehgtform('translate',Xc,Yc,0, 'zrotate',deg2rad(th), 'translate',-Xc,-Yc,0);
Coords = [Rg0(:), Cg0(:), zeros(numel(Rg0),1), ones(numel(Rg0),1)];
TrCoords = Coords*m.';
Rg{i} = reshape(TrCoords(:,1),size(Rg0));
Cg{i}= reshape(TrCoords(:,2), size(Cg0));
tg = griddata(R, C, F, Rg{i}, Cg{i});
tg(isnan(tg)) = 0;
Fg{i} = tg;
results(i) = trapz(trapz(Fg{i},2)) * (r0(2)-r0(1)) * (c0(2)-c0(1));
end
writematrix(results, 'integral_val.txt')
and when you do your plotting,
axis equal
which is not the default.
MS
MS el 5 de Mayo de 2020
Thank you very much.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Synthesis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by