For loop on .txt serie
Ältere Kommentare anzeigen
I have a time serie mySerie.txt (1000x1). I need to evaluate a function on consecutive subseries with increase in length, from 350 points up to 750 points. Example: starting from latest 350 points (from 651 : 1000, first serie) I have to apply myFunction and get a result,then do the same but this time on data from 650:1000, and again from 649 : 1000 and so on up to the last iteration on 251:1000.These are 750 iterations and I will get 750 result from myFunction, saved in an array or vector in order to be plotted.Thanks for any help.
Antworten (1)
BhaTTa
am 26 Jul. 2024
To evaluate a function on consecutive subseries with increasing lengths from a time series and collect the results for plotting, you can follow these steps in MATLAB:
- Load the time series data from the file.
- Define the range of subseries lengths you want to analyze.
- Iterate over the time series to extract the subseries and apply the function.
- Store the results in an array.
- Plot the results.
Here's a MATLAB script that accomplishes this:
% Step 1: Load the time series data
mySerie = load('mySerie.txt'); % Adjust the file path if needed
n = length(mySerie);
% Define the range of subseries lengths
minLength = 350; % Starting length
maxLength = 750; % Ending length
% Initialize an array to store the results
results = zeros(maxLength - minLength + 1, 1);
% Define your function handle (replace this with your actual function)
myFunction = @(data) mean(data); % Example function: mean of the subseries
% Iterate over the time series to extract subseries and apply the function
for length = minLength:maxLength
% Compute the starting index of the subseries
startIdx = n - length + 1;
% Extract the subseries
subSeries = mySerie(startIdx:end);
% Apply the function and store the result
results(length - minLength + 1) = myFunction(subSeries);
end
% Create an array of subseries lengths for plotting
lengths = minLength:maxLength;
% Plot the results
figure;
plot(lengths, results, '-o');
xlabel('Subseries Length');
ylabel('Function Result');
title('Function Results for Increasing Subseries Lengths');
grid on;
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!