For-looping output not adequate

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)

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

Hi Jan, Thanks for the comment. I am still learning MATLAB.
How can I predetermined the new_parameter(i-4) for calculating another_new_parameter?
Also, I noticed that the calculated 'new' and 'another_new_parameters' should be similar to T3 in terms of numbers of rows which is 120 rows as both i and T3 uses the same inputs which is extract the data from T2 at 4 rows interval. But I am getting 120 rows for T3 but 484 rows from i for-looping.
What are the possible causes for this?
Thanks for pointing out the subvector issue. I was not aware of it and it kept causing troubles.
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
T = 1×4
3 5 7 9
x(T) = 1
x = 1×9
0 0 1 0 1 0 1 0 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.
ack
ack on 18 Aug 2022
Edited: ack on 18 Aug 2022
Hi Jan, thank you so much. The problem is new_parameter vector has its own double values. If I do new_parameter(i) = 1, the result is every 4th elements became 1 and instead of becoming zeros, the values from other rows remain unchange.
How can I extract its origial values for every 4th element while converting the other rows into zeros (or create logical vector and index them to extract)? The current process did not yield like it. Thanks!
edit: I use T4 = T2(4:4:484); and got 121 vector. Is it the correct use of indexing to extract data from every 4th rows? Thanks.
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.
Hi Walter, thanks for pointing out. Yes, it is as you said 4 rows at a time. Is there anyway to extract data from every 4th rows and remove the others? Thanks.

Sign in to comment.

Products

Release

R2022a

Asked:

ack
on 1 Aug 2022

Commented:

on 18 Aug 2022

Community Treasure Hunt

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

Start Hunting!