Finding the closest value in an large array
10 views (last 30 days)
Show older comments
I have an array of 1001x1001. I would like to pull out a single value that is closest to 0 out of my "F" array. I do not know how to do this. Everytime I do it, it returns an entire column but I only want a single value.
The same thing happens for when I try to do the max of my "lambda". Would you please help me find the max and value closest to zero of these two variables. Thank you very much!
H_max = 20000*0.3048 ; % Meters
SM = 2;
amax = 10;
ML = 1 ; % kg
rhos = 2700 ; % kg/Meter^3
rhop = 1772 ; % kg/Meter^3
sigma = 60*10^6 ; % Pascal
N = 3 ; % Number of Fins
R = 287 ; % J/kg
T = 298 ; % Kelvin
g = 9.81 ; % Meters/Second^2
gamma = 1.4 ; % ND
a = (gamma*R*T)^(1/2) ; % Meters/Second
Pa = 101.325*10^3 ; % Pascal
% Solving for R_max, W_eq, t_b with linear burn rate
Rmax = 1 + amax;
Weq = ((H_max*g)/((log(Rmax)/2)*(log(Rmax)-2)+((Rmax -1)/(Rmax))))^(1/2);
Meq = Weq/a;
tb=((Rmax -1)*Weq)/(g*Rmax);
P0_Pa = (1+Meq^2*((gamma-1)/2))^(gamma/(gamma-1)) ; % ND - Pressure Ratio
P0 = P0_Pa*Pa ; % Pa - Pressure
% Starting with an Initial Lambda Max
lambdamax = 0;
D = 0:0.001:1; % i
L = 0:0.001:1; % j
% Preallocating each variable
delta = zeros(size(D));
r = zeros(size(D));
Mfb = zeros(size(D));
Mn = zeros(size(D));
Mcone = zeros(size(D));
Mcyl = zeros(size(D));
Mfin = zeros(size(D));
Ms = zeros(size(D));
M0 = zeros(size(D));
Mp = zeros(size(D));
Lp = zeros(size(D));
Xcp = zeros(size(D));
Xcg = zeros(size(D));
xp = zeros(size(D));
lambda = zeros(size(D));
F = zeros(size(D));
for i = 1:length(D)
for j = 1:length(L)
delta(i) = D(i)*P0/(2*sigma);
r(i) = D(i)/2;
Mfb(i) = pi*D(i)*rhos*D(i)*delta(i) ;
Mn(i) = delta(i)*rhos*pi*(D(i)/2)*((D(i)/2)+(D(i)^2+ (D(i)^2)/4)^(1/2));
Mcone(i) = rhos*delta(i)*(pi*r(i)*(r(i)+r(i)^2)^(1/2));
Mcyl(i,j) = rhos*pi*D(i)*delta(i)*L(j);
Mfin(i) = rhos*D(i)^2*delta(i)*pi +(3/2)*rhos*delta(i)*D(i)^2;
Ms(i,j) = (Mcyl(i,j) + Mfin(i) + Mcone(i));
Mp(i,j) = (Rmax - 1)*(Ms(i,j)+ML);
Lp(i,j) = Mp(i,j)/(pi*D(i)^2*rhop/4);
Ckn = (4*N*(4/3))/(1+sqrt(6)) ; % ND
Xcp(i,j) = (1.33*D(i) + Ckn*(D(i)+L(j)+(D(i)/3)))/(2+Ckn) ; % Meters
xp(i,j) = 2*D(i)+L(j)-Lp(i,j)/2;
Xcg(i,j) = ((2/3)*D(i)*Mn(i) + (2/3)*D(i)*ML + (D(i)+L(j)/2)*Mcyl(i,j)...
+ Mp(i,j)*xp(i) +((3*D(i)+2*L(j))/2)*Mfin(i))/(Mfin(i) + Mp(i,j)...
+ ML+Mcyl(i,j)+Mn(i));
lambda(i,j) = ML/(Ms(i,j)+Mp(i,j));
F(i,j) = Xcp(i,j)-Xcg(i,j)-D(i)*SM;
end
end
0 Comments
Accepted Answer
Ameer Hamza
on 9 May 2020
You need to specify 'all' to min() and max() to find the minimum over the entire matrix. For example, this returns the minimum value and its location
[min_val, index] = min(abs(F-0), [], 'all', 'linear');
[row, col] = ind2sub(size(F), index);
4 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!