How to compare two arrays and logically replace elements to satisfy my condition

5 views (last 30 days)
I have two arrays that are physically related. I want to compare it column by column (ie. column 1 from array 1 compared to column 1 from array 2) and replace values in array 1 with the value of array 2 if the array 1 value is greater than the value in array 2. This logic part is seen at the end of my code
Thanks!
%% Monte Carlo Simulations for PS Flow Rate
n = 100000;
g = 32.2; %ft/s^2
Cd = 0.94; %Get engineering judgment for this
af = [0.01 0.95 0.99]; %NF, PF, CF Percentage of what is left. Differentiates damage states. this is what the flow area is
% Randomly generate flood heights for each flood height discretization bin
delta_h = [rand(n,1)*3, rand(n,1)+3, rand(n,1)+4, rand(n,1)+5, rand(n,1)+6, rand(n,1)+7, rand(n,1)+8, rand(n,1)+9, rand(n,1)+10, rand(n,1)+11, rand(n,1)+12, rand(n,1)+13, rand(n,1)+14, (rand(n,1)*5)+15] ; %Uniform random numbers from 0 to 3
Q_cf=[];
Q_pf=[];
Q_nf=[];
for i = 1:14
Q_cf = [Q_cf Cd.*sqrt(2.*g.*delta_h(:,i)).*af(3)];
Q_pf = [Q_pf Cd.*sqrt(2.*g.*delta_h(:,i)).*af(2)];
Q_nf = [Q_nf Cd.*sqrt(2.*g.*delta_h(:,i)).*af(1)];
end
Q_edges = [0 13 20 25 35];
q_cf_histcount = [];
q_pf_histcount = [];
q_nf_histcount = [];
for i = 1:14
q_cf_histcount = [q_cf_histcount; histcounts(Q_cf(:,i),'BinEdges',Q_edges,'Normalization','probability')];
q_pf_histcount = [q_pf_histcount; histcounts(Q_cf(:,i),'BinEdges',Q_edges,'Normalization','probability')];
q_nf_histcount = [q_nf_histcount; histcounts(Q_cf(:,i),'BinEdges',Q_edges,'Normalization','probability')];
end
%% Internal Flood Height
time = 3600; %Flood time duration taken as 1 hour or 3600 seconds
l_int = 25; %Length of room [ft]
w_int = 25; %Width of room [ft]
A_int = l_int*w_int;
h_int_cf = [];
h_int_pf = [];
h_int_nf = [];
h_int_cf = [h_int_cf (Q_cf.*time)/A_int];
h_int_pf = [h_int_pf (Q_pf.*time)/A_int];
h_int_nf = [h_int_nf (Q_nf.*time)/A_int];
%% Compare h_int to delta_h and replace h_int if greater than delta_h
for i = 1:14
h_int_cf = (h_int_cf(:,i)>=delta_h(:,i));
h_int_pf = (h_int_pf(:,i)>=delta_h(:,i));
h_int_nf = (h_int_nf(:,i)>=delta_h(:,i));
end

Accepted Answer

Voss
Voss on 8 Apr 2022
"replace values in array 1 with the value of array 2 if the array 1 value is greater than the value in array 2"
% if array1 > array2
% array1 = array2;
% end
% therefore:
% array1 = min(array1,array2) % operates element-by-element
h_int_cf = min(h_int_cf,delta_h);
h_int_pf = min(h_int_pf,delta_h);
h_int_nf = min(h_int_nf,delta_h);

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!