# Multiple outputs from a for loop

조회 수: 1(최근 30일)
Roger Kalvig 2022년 6월 21일
편집: Stephen23 2022년 6월 21일
Basic problem, but I can't find the solution:
consider basic a for loop:
for i = 1:5
z(i)=3*i
end
It produces a vector z= [3 6 9 12 15]. What I need is all the components of the vector z as a sperate variables(scalars), let,s say
z1=3, z2=6 and so on. How can I do that?
Roger Kalvig
##### 댓글 수: 4표시숨기기 이전 댓글 수: 3
Stephen23 2022년 6월 21일

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

### 채택된 답변

Stephen23 2022년 6월 21일
편집: Stephen23 2022년 6월 21일
Rather than distracting with anti-pattern numbered variables (like you asked about), instead you should just use basic, normal, efficient, easy, simple, standard indexing. Indexing is how MATLAB works best.
d = 5;
xc = -100:10:280; % (some range suiting fcn function)
iv = nan(1,numel(xc));
fh = @(x) fcn(x) .* myGausstest(x);
for k = 1:numel(xc)
% do integration here of the product of both functions fcn*myGausstest for integral limts (xc-d,xc+d)" using trapz or integral
iv(k) = integral(fh,xc(k)-d,xc(k)+d);
end
Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is 6.7e-03. The integral may not exist, or it may be difficult to approximate numerically to the requested accuracy.
Warning: Minimum step size reached near x = 180. There may be a singularity, or the tolerances may be too tight for this problem.
plot(xc,iv)
function y = fcn(x)
Bext = 0.6;
G = 10.553;
y = 1./(G.*Bext.*sind(x));
y(x<0) = 0;
y(x==0) = 100;
y(x==180) = 100;
y(x>180) = 0;
end
function y = myGausstest(x)
A = 10;
w = 20;
xc = 90;
y = A.*exp(-0.5*((x-xc)./w).^2);
end
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Roger Kalvig 2022년 6월 21일
Thanks very much for help

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

### Community Treasure Hunt

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

Start Hunting!

Translated by