For-looping output not adequate
Show older comments
Hi, I would like the for-looping to extract the data from T2 variable at 4 row intervals from the last one which is 484 row to 480 to 476 etc. The current code is not extracting the data out of T2. How can I fix this? Thanks.
%from struct to matrix using function
REC = load('C:\Users\ayech\Data\COMPLETE.mat');
T1 = createDataMatrix_revised3(REC);
%transposing the matrix to turn the variables into column
T1=T1';
%removing Nan for data pre-treatment
y=any(isnan(T1),2);
T1(y,:)=[];
mult = 1 /0.5; %
time = 2;%time step 2sec
T2 = T1(:,1); %first column extraction out of T1
T3 = length(T2):-(time*mult):5; %random check
for i = length(T2):-(time*mult):5 %struct array?
new_parameter (i) = T2(i) - T2(i-(time*mult));
another_new_parameter (i) = new_parameter (i) - new_parameter(i-(time*mult));
new_parameter_max = max(T2((i-time*mult):i));
new_parameter_min = min(T2((i-time*mult):i));
new_parameter_mean = mean(T2((i-time*mult):i));
end
Answers (1)
Jan
on 1 Aug 2022
new_parameter(i) = T2(i) - T2(i-(time*mult));
In the first iteration the i.th element of the vector new_parameter is calculated.
another_new_parameter(i) = new_parameter(i) - new_parameter(i-(time*mult))
Then this line accesses new_parameter(i-4), which was not determined before. As default its value is 0.
Are you aware, that (i-time*mult):i contains 5 indices, such there is an overlap between the subvectors? Do you mean: (i-time*mult)+1:i ?
You do not need a loop to get the min, max and mean values over blocks of 4 elements:
x = rand(484, 1);
y = reshape(x, 4, []);
ymin = min(y, [], 1);
ymax = max(y, [], 1);
ymean = mean(y, 1);
6 Comments
ack
on 14 Aug 2022
for i = length(T2):-(time*mult):5
new_parameter(i) = ...
end
After this loop, every 5th element of the vector new_parameter has a value and the rest are zeros. Example:
T = 3:2:10
x(T) = 1
"How can I predetermined the new_parameter(i-4) for calculating another_new_parameter?" - by defining new_parameter(i-4) before it is used.
Walter Roberson
on 18 Aug 2022
mult = 1 /0.5; %
time = 2;%time step 2sec
mult would be 2, time would be 2
for i = length(T2):-(time*mult):5
mult*time would be 2*2, =4
so you would be going backwards 4 at a time, not 5 at a time.
ack
on 18 Aug 2022
Walter Roberson
on 18 Aug 2022
subset = data(1:4:end,:);
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!