How can I create a nested loop without confusing the indeces?

I have the following problem:
a variable x(i) that represents the payoff and its value changes as other variables cgange WHILE I KEEP FIXED alpha. So I can get it by using a simple loop like that
alpha = 0.3; % between 0 and 1
x = zeros(N,1);
for i=1:N
x(i) = alpha*exp(z(i))+(1-alpha)*exp(u(i));
end
I don't know how to get different paths of x(i) as alpha changes

답변 (1개)

Let's see if I understand the intent... I don't know what z and u are, so I'm just going to use placeholders.
N = 10;
alpha = [0.2 0.3 0.5]; % a vector of alpha
z = rand(N,1);
u = rand(N,1);
x = alpha.*exp(z) + (1-alpha).*exp(u)
x = 10×3
1.1978 1.2503 1.3553 2.1130 2.0850 2.0290 1.2143 1.2290 1.2583 1.1931 1.2798 1.4532 2.1470 2.0129 1.7446 1.4468 1.5637 1.7975 1.8198 1.7721 1.6766 2.2546 2.1952 2.0765 1.8337 1.7453 1.5686 1.1697 1.1559 1.1285
Note that alpha and the other vectors are orthogonal. Implicit array expansion allows the expression to be evaluated for all values of the parameter alpha (one column each).

댓글 수: 3

Exactly, z and u are vectors too. If I proceed in this way, like you showed me, I get that alpha varies while z and u varie too. Instead I want to get at first 10 values of x that I get because z and u varie but by using only the first value of alpha (so alpha=0.2); then other 10 values of x by using alpha=0.3 and so on... in a recursively way. Anyway I think that I have to use two different indeces in the loop (i and j) and then I associate j to alpha and i to other variables
DGM showed you the vectorized way, not recursive. However you can make up a function that takes alpha as in input if you want. However you'd just call it as you need to (with the new alpha) -- you don't need to call the function recursively, and you should not.
That's what the example does. I don't see the functional difference between doing the operations sequentially or simultaneously. For demonstration:
% SETUP
N = 10;
alpha = [0.2 0.3 0.5]; % a vector of alpha
z = rand(N,1);
u = rand(N,1);
% USING LOOPS
x = zeros(N,numel(alpha));
for ia = 1:numel(alpha)
for ix = 1:N
x(ix,ia) = alpha(ia)*exp(z(ix)) + (1-alpha(ia))*exp(u(ix));
end
end
% USING VECTORIZED MATH
x2 = alpha.*exp(z) + (1-alpha).*exp(u);
immse(x,x2) % show that the results are identical
ans = 0
Perhaps instead z and u are larger than you want the output to be, and you only want to sample a portion of them.
% SETUP
N = 10;
alpha = [0.2 0.3 0.5]; % a vector of alpha
z = rand(100,1); % larger than N
u = rand(100,1);
% USING LOOPS
x = zeros(N,numel(alpha));
for ia = 1:numel(alpha)
for ix = 1:N
x(ix,ia) = alpha(ia)*exp(z(ix)) + (1-alpha(ia))*exp(u(ix));
end
end
% USING VECTORIZED MATH
x2 = alpha.*exp(z(1:N)) + (1-alpha).*exp(u(1:N));
immse(x,x2) % show that the results are identical
ans = 0
There is the possibility that you want x to remain a vector, and that each evaluation for alpha should simply be concatenated to the end. In that case, just do this to the result:
x = x(:);
% or
x = reshape(x,[],1);
That will reshape the multicolumn output into a single column vector.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2021년 9월 30일

댓글:

DGM
2021년 10월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by