MATLAB code requires a loop explanation
Show older comments
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
Accepted Answer
More Answers (2)
% 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);
1 Comment
Rizwan Niaz
on 5 Mar 2022
Image Analyst
on 4 Mar 2022
0 votes
Take the semicolons off the lines and step through it one line at a time in the debugger and see how the variables change.
1 Comment
Rizwan Niaz
on 4 Mar 2022
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!