Multiple outputs from a for loop

조회 수: 1(최근 30일)
Roger Kalvig
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
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?
Thanks in advance
Roger Kalvig
  댓글 수: 4

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

채택된 답변

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);
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.
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;
function y = myGausstest(x)
A = 10;
w = 20;
xc = 90;
y = A.*exp(-0.5*((x-xc)./w).^2);
  댓글 수: 3
Roger Kalvig
Roger Kalvig 2022년 6월 21일
Thanks very much for help

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

추가 답변(0개)


Community Treasure Hunt

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

Start Hunting!

Translated by