simple nested loops error

조회 수: 4 (최근 30일)
MS
MS 2020년 4월 27일
댓글: MS 2020년 5월 5일
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

채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 27일
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
MS
MS 2020년 4월 28일
편집: MS 2020년 4월 28일
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 2020년 4월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

MS
MS 2020년 4월 28일
편집: Walter Roberson 2020년 4월 28일
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
Walter Roberson
Walter Roberson 2020년 5월 4일
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 2020년 5월 5일
Thank you very much.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by