MATLAB: Maximum value is incorrect

maximum

I have this code and I want to calculate the maximum value of alpha but it's giving me the minimum value:
phi = 53;
h = 1/24:1/24:1;
d = 100:100:365;
for i = 1:1:length(d)
delta(i) = -23.45 * cosd((360/365) * (d(i)+10));
for j = 1:1:length(h)
h_a(j) = 360 * (h(j)-0.5);
alpha(i,j) = asind(((sind(phi) * sind(delta(i))) + (cosd(phi) * cosd(delta(i)) * cosd(h_a(j)))));
theta(i,j) = 90 - alpha(j);
if h <= 0.5
beta(i,j) = -acosd((((sind(delta(i)) * cosd(phi)) - (cosd(h_a(j)) * cosd(delta(i)) * sind(phi)))/sind(theta(j))));
else
beta(i,j) = acosd((((sind(delta(i)) * cosd(phi)) - (cosd(h_a(j)) * cosd(delta(i)) * sind(phi)))/sind(theta(j))));
end
end
end
a = max(alpha(i,j));
fprintf('%.1f\n',a);
disp(a)

Best Answer

After your for j loop, j will have a value that is the last value assigned to it in the loop; for j=1:1:length(h) will leave j=length(h) after the loop. .... quoting myself from an earlier response to you.
After your for i loop, i will have a value that is the last value assigned to it in the loop; for i=1:1:length(d) will leave i=length(d) after the loop.
So after the loop, i will be length(d) and j will be length(h) . Both of those are scalars.
So when you ask max(alpha(i,j)) you are asking for max() of a scalar. Which is just going to give you the scalar back.
You need to decide whether you want the maximum for each row, or for each column, or over the entire array. Please read the documentation for max() and pay attention to the "dim" parameter.