- read in the data
- find out how many entries we got
- we want to take the data and put it across 12 rows
- but what happens if the original data was not an exact multiple of 12 entries?
- we figure out how many more entries would be needed to take us to a multiple of 12
- then we pad the data with that many nan
- so now T is an exact multiple of 12 items long
- take consecutive entries from T, and arrange them in 12 rows with as many columns as needed
- once we have that, transpose the data so we now have 12 columns and as many rows as needed
MATLAB code requires a loop explanation
3 views (last 30 days)
Show older comments
Rizwan Niaz
on 4 Mar 2022
Commented: Rizwan Niaz
on 5 Mar 2022
I am very new in MATLAB programming, I am trying several examples from google, I understood several codes, however, a code example I need to understand for starting my further, analysis. can you please explain the equation or meaning of this written code in a loop? what is the mathematics or meaning of this? below in command I get T is data that we took from excel. secondly, n is the length of data, next yt= n/12 fine. What is there in for loop? I will appreciate your answer. thank you so much. I run data examples and found output from this code but I am unable to understand the mathematics, equation or meaning of this that use in for loop for generating the data. For example, my data length is 732, then yt=n/12=61, in ys=(fix((i-1)/12))+1; what code want to get, why it define while k>12 and so on,
T=xlsread('ali.xls','temp','C:C');
n=length(T);
yt=n/12;
for i=n:-1:1
ys=(fix((i-1)/12))+1;
k=i;
while k>12
k=k-12;
end
m=k;
X(m,ys)=T(i);
end
0 Comments
Accepted Answer
Walter Roberson
on 4 Mar 2022
k=i;
while k>12
k=k-12;
end
m=k;
That code is equiavelent to
m = mod(k-1, 12) + 1;
Another way of writing the code would be:
T = xlsread('ali.xls','temp','C:C');
n = length(T);
padding_needing = mod(-n, 12);
T = [T; nan(padding_needed, 1)];
X = reshape(T, 12, []).';
This says:
In the special case where we were sure that the data had an exact multiple of 12 entries, then this could all be simplified to
T = xlsread('ali.xls','temp','C:C');
X = reshape(T, 12, []).';
5 Comments
Walter Roberson
on 5 Mar 2022
for i=n:-1:1
In context, taking into account the fact that some unusual behaviour is not coded in that loop, that code is equivalent to:
i = [];
if n >= 1
hidden_temporary_variable = n;
while hidden_temporary_variable >= 1
i = hidden_temporary_variable;
execute the code for the loop body
hidden_temporary_variable = hidden_tempory_variable - 1;
end
end
Or if you just want the summary: count i backwards from n to 1, leaving i as 1 afterwards.
Walter Roberson
on 5 Mar 2022
The rest, other than the assignment statement, has to do with figuring out how many full groups of 12, and how far you are into that group.
The logic could have been written like
i = [1;2;3;11;12;13;23;24;25];
m = mod(i-1, 12) + 1;
ys = (i - m)/12 + 1;
[i, m, ys]
So m is how many months into the year you are, and ys is how many years in you are.
More Answers (2)
Voss
on 4 Mar 2022
Edited: Voss
on 4 Mar 2022
% T=xlsread('ali.xls','temp','C:C');
T = (1:100).'
n=length(T);
yt=n/12;
for i=n:-1:1 % this for loop populates the matrix X with elements
% from T (starting with the last element of T and ending with
% the first, but the order doesn't matter in this algorithm).
% fix() rounds toward 0.
% fix((i-1)/12) rounds (i-1)/12 toward 0.
% since i is positive in this case, this is equivalent to
% floor((i-1)/12), which rounds to the largest integer smaller than or
% equal to (i-1)/12:
% i (i-1)/12 floor((i-1)/12)
% -------------------------------
% 1 0/12 0 [ 12 zeros ]
% 2 1/12 0
% 3 2/12 0
% ... ... 0
% 12 11/12 0
% 13 12/12 1 [ 12 ones ]
% ... ... 1
% 24 23/12 1
% 25 24/12 2 [ 12 twos ]
% ... ... 2
%
% so fix((i-1)/12)+1 is one more than the values in the right-hand
% column in the table above: twelve 1's followed by twelve 2's and so
% on, as i increases from 1.
%
% this number will be the column in X that T(i) will go to.
ys=(fix((i-1)/12))+1;
% find the integer m (1 <= m <= 12) that is congruent to the given integer i, modulo 12.
% [i mod 12 is the remainder when some number i is divided by 12. for example:
% 0, 12, 24, 36, ... are all congruent to 0 mod 12 (these are multiples of 12), and
% 1, 13, 25, 37, ... are all congruent to 1 mod 12 (1 more than a multiple of 12) and
% 2, 14, 26, 38, ... are all congruent to 2 mod 12 (2 more than a multiple of 12),
% and so on, up to
% 11, 23, 35, 47, ... are all congruent to 11 mod 12 (11 more than a multiple of 12).
k=i; % start with i, an integer
while k>12 % and as long as it is more than 12
k=k-12; % keep subtracting 12
end
m=k; % now you have an integer between 1 and 12, inclusive.
% this number is the row in X that T(i) will go to.
% set the element in row m, column ys of matrix X to T(i) (the ith
% element of T):
X(m,ys)=T(i);
end
disp(X);
Image Analyst
on 4 Mar 2022
Take the semicolons off the lines and step through it one line at a time in the debugger and see how the variables change.
See Also
Categories
Find more on Loops and Conditional Statements 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!