loop not recording array

I am trying to get the IF statement to record which numbers when the "Differences" function is higher than the avgdiff
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
floods = [];
count = 1;
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^
differences = raininches(n) - raininches(n - 1);
% Takes each years rain in inches and subtracts them from the previous
% year to find the difference between each years rain fall^
avgdiff = mean(differences);
% finds the Average difference accross all recorded years^
if differences > avgdiff
% Loops to check if those 2 years difference is higher than the average
% difference over the recorded years
floods(count) = [n,1];
% If it is higher, it records the difference in an Array here^
end
count = count + 1;
end
yline(avgdiff,'r')
% Plots the Average Difference of all the years combined^
legend('Rain each year','Average Difference over time')
% Creates a legend for both plots^
hold off
x = dates(floods(count),1);
fprintf('Each of these years have a higher probability of flash floods: \n', x)

Answers (2)

for n = 2:length(raininches)
n will be a scalar inside the loop.
differences = raininches(n) - raininches(n - 1);
scalar indexing minus scalar indexing: the result will be a scalar.
avgdiff = mean(differences);
mean() of a scalar will be the scalar itself.
if differences > avgdiff
Because the mean of a scalar is the scalar itself, it is not possible for the scalar in differences to be greater than the (same) scalar in avgdiff

4 Comments

Tommy
Tommy on 1 Dec 2023
Edited: Tommy on 1 Dec 2023
so I need to get the mean of all of the differences combined first THEN find out if each year has a higher difference than the average difference accross all numbers? I think I see what you mean now. So do I need to use 2 if statments and record the difference in an array first?
Note that if statements are not referred to as "loops".
The body of an if statement is executed exactly 0 or 1 times.
In MATLAB, the body of a for loop is executed a fixed number of times determined by the range or size of the expression -- unless something exits the loop early.
In MATLAB, the body of a while loop is executed an indefinite number of times (possibly 0) until a condition is no longer satisfied -- unless something exits the loop early. The condition is tested first before executing the loop body.
Some other programming languages offer additional looping possibilities, including do while which executes the body at least once and continues executing the body until the given test fails (MATLAB does not offer do while )
if could have been called a "loop", but because it never does the body repeatedly, in English it is not called a "loop".
ok so the qustion is how do I record values from an if statement, inside a for loop and place them in an array i can use?
You can use a loop to calculate the difference at a "current" position and record that difference in an array. You can add the current difference to a total difference that you are accumulating. After the loop you can divide the total difference by an appropriate number to determine the average difference.
After that you can loop over the already-recorded current differences and compare each of them to the calculated average difference, and do whatever is appropriate

Sign in to comment.

madhan ravi
madhan ravi on 1 Dec 2023
Edited: madhan ravi on 1 Dec 2023
Note: Handcoded from memory. Loops are unnecessary in this case.
differences = diff(raininches);
avgdiff = mean(differences);
x = dates(differences > avgdiff);

1 Comment

Sadly I need to utilize a loop for this code

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 1 Dec 2023

Commented:

on 1 Dec 2023

Community Treasure Hunt

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

Start Hunting!