Hi, I have the nested FOR loops here-under and I want to save all the outputs of the "P_current" variable in a multidimensional array P(mm, kk, pp). Problem is; the values of P(mm, kk, pp) get overwritten and only the values which result from the last iteration of the FOR loop "for ww = 1:nwall" stay. Let me know if you need the embedded functions' codes, though I believe that it doesn't matter.
for mm = 1:nrx_x
for kk = 1:nrx_y
prx_current = [X(mm, kk); Y(mm, kk)];
for pp = 1:ntx
ptx_current = ptx(:, pp);
d = vmag(prx_current, ptx(:, pp));
d(d==0) = eps;
P_current = Pt .* (lambda ./ (4*pi.*d)).^2;
d(mm,kk,pp) = d;
for ww = 1: nwall
x = intersect3 (wwallStart(:,ww), wwallEnd(:,ww), ptx(:,pp), prx_current);
if x(1,1) >= 0 && x(1,1) <= 1 && x(2,1) >= 0 && x(2,1) <=1
P(mm, kk, pp) = P_current * gwall;
else
P(mm, kk, pp) = P_current;
end
end
end
end
end

Antworten (1)

Stephen23
Stephen23 am 29 Mär. 2017
Bearbeitet: Stephen23 am 29 Mär. 2017

0 Stimmen

There are four loops, and only three indices are used:
for mm = ...
for kk = ...
for pp = ...
for ww = ...
P(mm, kk, pp) = ...
end
end
end
end
Of course only the last ww value appears in the output, because you never use the ww index to allocate to different locations of your output array: every ww iteration is allocated to the same location in the array.

3 Kommentare

Sherif Elkholy
Sherif Elkholy am 29 Mär. 2017
Thanks Stephen. So any idea how can I store all the iterations of "P_current" but not P(mm,kk,pp,ww)? I tried P(mm,kk,pp,ww) but it doesn't give me what I want.
Jan
Jan am 29 Mär. 2017
@Sherif: Please explain what you want instead.
Sherif Elkholy
Sherif Elkholy am 29 Mär. 2017
I need the values of P(mm,kk,pp) which resulted from the mm, kk, pp loops not affected which I managed to before inserting the ww loop. Then I need the values of P(mm,kk,pp) whose location in the matrix satisfies the condition in the ww loop changed according to the statement P(mm,kk,pp) = P_current * gwall. I hope I made myself clear.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 29 Mär. 2017

Kommentiert:

am 29 Mär. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by