Only the last value are being stored in for loop

1 次查看(过去 30 天)
Hi all,
I just need to have a and c store array of numbers rather than store the last number. I need to sample from [a,c] and a should be something like [0 0.5 1 1.5...] and c should be [0.5 1 1.5 2...] but as of right now a and c just take the last values, 5 and 20.
Nsim = 10000;
IBinEdges=[0:0.5:5 20]; %ft
IBinEdges_Lo=IBinEdges(1:end-1);
IBinEdges_Hi=IBinEdges(2:end);
IBinEdges_Md=IBinEdges_Lo+(diff(IBinEdges)/2);
%This is external flood (nominal) elevation
ExtSurgeBinEdges = [0:0.5:5 20];
ExtSurgeBinEdges_Lo=ExtSurgeBinEdges(1:end-1);
ExtSurgeBinEdges_Hi=ExtSurgeBinEdges(2:end);
ExtSurgeBinEdges_Md=ExtSurgeBinEdges_Lo+(diff(ExtSurgeBinEdges)/2);
% Generation every combination of external flood height and seal state
% iterations
IndF=[1:length(ExtSurgeBinEdges_Md)]';
IndA=[1:length(DamState)]';
temp1=[]; temp2=[]; [temp1,temp2]=ndgrid(IndF,IndA);
IndMatFA=[temp1(:),temp2(:)];
% col1: surge height
% col2: seal condition
% This is where the issue is, a and c needs to contain each bin edge for
% ExtSurgeBinEdges low and high. it just has the extremes
% Use MC simulation to generate CPT for IF node
for iComb=1:size(IndMatFA,1) % loop over combinations of damage state and external flood height
% Define distribution of external flood height within bin and sim:
a=[]; a=ExtSurgeBinEdges_Lo(IndMatFA(iComb,1));
b=[]; b=ExtSurgeBinEdges_Md(IndMatFA(iComb,1));
c=[]; c=ExtSurgeBinEdges_Hi(IndMatFA(iComb,1));
pd1=[]; pd1=makedist('Uniform',a,c);
Fsim=[]; Fsim=random(pd1,[Nsim,1]); %simulate external flood heights within bin (Uniform)
Fsim=Fsim.*(Fsim>=0); % make sure the difference is never below zero
end
  1 个评论
VBBV
VBBV 2023-4-10
编辑:VBBV 2023-4-10
Isnt this supposed to be outside of loop ?
% iComb=1:size(IndMatFA,1);
a=[];
b=[];
c=[];
pd1=[];
Fsim=[];
Can you tell which difference is never below zero ? Do you mean successive Fsim values generated using uniform distribution ?
% which Difference is never below zero ? this line is confusing
Fsim=Fsim.*(Fsim>=0); % make sure the difference is never below zero

请先登录,再进行评论。

采纳的回答

VBBV
VBBV 2023-4-6
编辑:VBBV 2023-4-6
a(iComb)=ExtSurgeBinEdges_Lo(IndMatFA(iComb,1));
b=[]; % put this line outside of loop
b(iComb)=ExtSurgeBinEdges_Md(IndMatFA(iComb,1));
c=[]; % this too
c(iComb)=ExtSurgeBinEdges_Hi(IndMatFA(iComb,1));
  3 个评论
Joy Shen
Joy Shen 2023-4-10
@VBBV I did that and I see that it fixed my issue with saving the values for a and c, but the pd1 makedist isn't registering it and still show up as the original single values. Here's my new code. I defined iComb and moved a, b and c outside of the loop. In pd1 I'm tyring to index a and c by putting a(iComb) and c(iComb) but when I check pd1 it says a and c are still single values. I commented %*** where I made changes.
Basically, Fsim is not sampling from each bin I'm trying to define between the arrays of a and c.
Nsim = 10000;
IBinEdges=[0:0.5:5 20]; %ft
IBinEdges_Lo=IBinEdges(1:end-1);
IBinEdges_Hi=IBinEdges(2:end);
IBinEdges_Md=IBinEdges_Lo+(diff(IBinEdges)/2);
%This is external flood (nominal) elevation
ExtSurgeBinEdges = [0:0.5:5 20];
ExtSurgeBinEdges_Lo=ExtSurgeBinEdges(1:end-1);
ExtSurgeBinEdges_Hi=ExtSurgeBinEdges(2:end);
ExtSurgeBinEdges_Md=ExtSurgeBinEdges_Lo+(diff(ExtSurgeBinEdges)/2);
% Generation every combination of external flood height and seal state
% iterations
IndF=[1:length(ExtSurgeBinEdges_Md)]';
IndA=[1:length(DamState)]';
temp1=[]; temp2=[]; [temp1,temp2]=ndgrid(IndF,IndA);
IndMatFA=[temp1(:),temp2(:)];
% col1: surge height
% col2: seal condition
% This is where the issue is, a and c needs to contain each bin edge for
% ExtSurgeBinEdges low and high. it just has the extremes
% Use MC simulation to generate CPT for IF node
%***
iComb=1:size(IndMatFA,1);
a=[]; a=ExtSurgeBinEdges_Lo(IndMatFA(iComb,1));
b=[]; b=ExtSurgeBinEdges_Md(IndMatFA(iComb,1));
c=[]; c=ExtSurgeBinEdges_Hi(IndMatFA(iComb,1));
for iComb=1:size(IndMatFA,1) % loop over combinations of damage state and external flood height
% Define distribution of external flood height within bin and sim:
pd1=[]; pd1=makedist('Uniform',a(iComb),c(iComb)); %***
Fsim=[]; Fsim=random(pd1,[Nsim,1]); %simulate external flood heights within each bin (Uniform)
Fsim=Fsim.*(Fsim>=0); % make sure the difference is never below zero
end
VBBV
VBBV 2023-4-10
编辑:VBBV 2023-4-10
Nsim = 10000;
IBinEdges=[0:0.5:5 20]; %ft
IBinEdges_Lo=IBinEdges(1:end-1);
IBinEdges_Hi=IBinEdges(2:end);
IBinEdges_Md=IBinEdges_Lo+(diff(IBinEdges)/2);
%This is external flood (nominal) elevation
ExtSurgeBinEdges = [0:0.5:5 20];
ExtSurgeBinEdges_Lo=ExtSurgeBinEdges(1:end-1);
ExtSurgeBinEdges_Hi=ExtSurgeBinEdges(2:end);
ExtSurgeBinEdges_Md=ExtSurgeBinEdges_Lo+(diff(ExtSurgeBinEdges)/2);
% Generation every combination of external flood height and seal state
% iterations
IndF=[1:length(ExtSurgeBinEdges_Md)]';
IndA=[1:length(DamState)]';
temp1=[]; temp2=[]; [temp1,temp2]=ndgrid(IndF,IndA);
IndMatFA=[temp1(:),temp2(:)];
% col1: surge height
% col2: seal condition
% This is where the issue is, a and c needs to contain each bin edge for
% ExtSurgeBinEdges low and high. it just has the extremes
% Use MC simulation to generate CPT for IF node
% isnt this supposed to be as in a loop ??
% iComb=1:size(IndMatFA,1);
a=[];
b=[];
c=[];
pd1=[];
Fsim=[];
for iComb=1:size(IndMatFA,1)
% loop over combinations of damage state and external flood height
% Define distribution of external flood height within bin and sim:
a(iComb)=ExtSurgeBinEdges_Lo(IndMatFA(iComb,1));
b(iComb)=ExtSurgeBinEdges_Md(IndMatFA(iComb,1));
c(iComb)=ExtSurgeBinEdges_Hi(IndMatFA(iComb,1));
pd1=makedist('Uniform','Lower',a(iComb),'Upper',c(iComb)); %***
Fsim(:,iComb)=random(pd1,[Nsim,1]); %simulate external flood heights within each bin (Uniform)
end
% check if the difference is never below zero
Fsim=Fsim.*(Fsim>=0); % make sure the difference is never below zero

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Help and Support 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by