Array not saving correct number after IF function
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
my question is on line 25 and on, I am trying to save the number count is on in an array when it meets the condition in the IF function but it isnt saving it. so wehn avgdiff is larger than (differences(2:113,1) + 2) I want it to save what number count is on and put it into array that I can pull from/ Any idea of where I am going wrong?
clc; clear;
rainfall = readmatrix('RainFall.txt');
% Reading the text file and turning it into a double^
dates = rainfall(:,1);
number = rainfall(:,2);
raininches = rainfall(:,3);
% Specifying the data we want from the text file^
plot(dates,raininches,'-o')
xlabel('years')
ylabel('Inches of rain')
% Plots the information above and labels the axis's
count = 0;
hold on
differences = zeros(length(raininches),1);
% Creates and array with a column to store the values of differences^
for n = 2:length(raininches)
% For loop starting at 2 so it can properly calculate the differences^
difference = raininches(n) - raininches(n - 1);
AbsoluteDiff = abs(difference);
differences(n) = AbsoluteDiff;
% Takes each years rain in inches and subtracts them from the previous
% year to find the difference between each years rain fall^
end
avgdiff = mean(differences);
floods = zeros(length(differences),1)
for j = 1:length(differences)
count = count + 1;
if (differences(2:113,1) + 6) > avgdiff
floods(count) = j;
end
end
yline(avgdiff,'r')
% Plots the Average Difference of all the years combined^
legend('Rain each year','Average Difference over all')
% Creates a legend for both plots^
hold off
0 Kommentare
Antworten (1)
Walter Roberson
am 2 Dez. 2023
differences = zeros(length(raininches),1);
% Creates and array with a column to store the values of differences^
for n = 2:length(raininches)
Note that differences is allocated to the full length of raininches. Note that the n loop starts with 2
differences(n) = AbsoluteDiff;
n starts at 2, so differences(2) is the first entry that will be stored into. differences(length(raininches)) is the last entry that will be stored into. differences(1) will retain the 0 value that the array was initialized with.
avgdiff = mean(differences);
Remember that differences starts with a 0, and is of length same as raininches. So you stored meaningful differences in entries 2 to length(raininches) and stored into something of length raininches, so the calculation of the mean will involve totaling and dividing by length(raininches). But there is that 0 at the beginning. There are length(inches) minus 1 "meaningful" values and one entry certain to be 0, but the mean will be over the entries including the 0... you are not taking mean(abs(diff(raininches)), you are taking mean([0, abs(diff(raininches))])
for j = 1:length(differences)
count = count + 1;
if (differences(2:113,1) + 6) > avgdiff
floods(count) = j;
end
end
The if test that is done does not depend upon the value of j or anything calculated in the loop. It will either be true based on information purely before the loop, or else it will be false based on information purely before the loop. So the body of the if will either never be executed, or else it will be executed every time. If it is never executed then floods will never change from 0. If it is executed every time then floods(1) will be assigned 1, floods(2) will be assigned 2, and so on.
if (differences(2:113,1) + 6) > avgdiff
differences(2:113,1) is a vector. Adding 6 to it will remain a vector. You are therefore testing a vector against the scalar avgdiff . When evaluating an if, MATLAB considers the result to be true only if every value being tested is non-zero. When using > the result is 0 for false and 1 for true. So if every (differences(2:113,1) + 6) is greater than avgdiff then the test will be considered true -- but if there is even one entry that is false then the entire test will fail.
Is it possible for it to be true for the entire vector? Yes. Suppose you let differences be randi(6, 50, 1) . Then mean() of differences would be somewhere in the range 1 to 6, not higher than 6. Meanwhile, adding 6 to those entries that are 1 to 6 would give results that are at least 7. As minimum value of 7 is greater than a value that is at most 6, you can see that in such a circumstance the test would be true no matter what the specific values in the vector.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!