for loop with 3 variables

xvirtual = [250:10:300]
for jj = 1:length(xvirtual)
for j = 50000:50100 %:length(Z(:,1))
for i = xvirtual(jj):length(xq(1,:))-1
V(j,i) = pointdistance*(Z(j,i)-Zq(i));
end
Volume(j) = sum(V(j,:) ,'omitnan'); % virtual overtopping volumes in time
end
Vis(jj,:) = Volume;
end
Hi hope someone can help me with this code. I want to loop through time=j, the space =i and different xvirtual levels = jj. As result I get Vis, however this result contains the same volumes for all rows. So probably it is only calculating for the first xvirtual=250. How can I calculate for the other values of xvirtual?

4 Comments

Why are you indexing j from 50000? This will create V and Volume variables vastly bigger than required, with 0s form the previous 49999 elements/rows.
Eventually I will index j from 1:79000. This was just to check if the script works.
Cris LaPierre
Cris LaPierre on 4 Feb 2019
Edited: Cris LaPierre on 4 Feb 2019
Hard to say without knowing more about the other variable: pointdistance, Z, Zq and xq. Can you attach a mat file containing them? Or at least provide info on their size?
Here I added a part of the workspace containing the variables. The following script applies for this workspace.
xvirtual = [250:10:300]
for jj = 1:length(xvirtual)
for j = 1:101 %:length(Z(:,1))
for i = xvirtual(jj):length(xq(1,:))-1
V(j,i) = pointdistance*(Z(j,i)-Zq(i));
if V(j,i)<0
V(j,i) = nan;
end
end
Volume(j) = sum(V(j,:) ,'omitnan'); % virtual overtopping volumes in time
end
Vis(jj,:) = Volume;
end

Sign in to comment.

 Accepted Answer

I would recommend that you avoid using more characters than you need to.
Xvirtual = 250:10:300; %no point in the []
length(xq(1, :)) is more simply written as size(xq, 2) and since xq is a vector, even more simply as numel(xq) (assuming it's always a vector).
You've told us that the code you've written produces the wrong result, but you haven't told us what the correct result should be. It's certainly not clear what your intent is.
At present the only dependence on xvirtual is the starting point of your i loop. So at jj = 1 you fill V from column i = 250 to column i = 591. At jj = 2, you fill V from column i = 260 to column i = 250, overwriting the values you'd written in the previous i step with the same values you'd already written. So yes, nothing change. Only the first jj step matters.
None of the loops you've written as required. and since for the jj loop, only the smallest value of xq matters, your current code could be rewritten as:
xvirtual = 250:10:300;
startpoint = min(xvirtual);
V = zeros(size(Z));
V(:, startpoint:end-1) = pointdistance * (Z(:, startpoint:end-1) - Zq(startpoint:end-1));
V(V < 0) = NaN;
Vis = sum(V, 2, 'omitnan')

1 Comment

Thank you very much for your help it works !!!! That is way easier to write the code and now I can loop through the xvirtual.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2017a

Asked:

on 4 Feb 2019

Commented:

on 5 Feb 2019

Community Treasure Hunt

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

Start Hunting!