Info
This question is closed. Reopen it to edit or answer.
Storing Vectors into Matrix from loop Issue
1 view (last 30 days)
Show older comments
Hello, I am having a problem with a loop. I have an excel file that is 480x13 which I imported in Matlab(I believe the dimensions are needed to help, if not ignore them). I then implemented the following code:
[data,strings] = xlsread('helpme.xlsx');
series = strings(1,2:end);
dates = strings(2:end,1);
Yc = cumsum(data(:,3));
PP_Tbl = [];
for i = [1,2,4,5,6,7,8,9,10,11,12,13];
X_pp = data(:,i);
for j = [1,3,6,12,24,36,48];
Y_sr = Yc(j+1:end);
X_sp = X_pp(1:end-j,:);
PP_Tbl = [PP_Tbl; Y_sr X_sp];
end;
end;
I am trying to store each iteration in the matrix PP_Tbl as a column, so the first column of PP_Tbl would be Y_sr for j=1, the second column X_sp for j=1, the third Y_sr for j=3, the fourth X_sp for j=3, and so on for all values of j and then i.
In total this would be 168 columns and 480 rows. But instead I get just two columns and 38,760 rows.
BTW, I purposefully ignored 3 for the first loop because Y_sr is always the 3rd column. If Y_sr is always the same, why not take it out you ask? Good question, what I'm trying to achieve with the PP_Tbl is to later use it for a fitlm function, which would use column 1 and 2, and then 3 and 4, so on in pairs until 167 and 168.
Is this feasible?
Thanks
0 Comments
Answers (1)
David Ding
on 28 Apr 2017
Hi Luis,
In order to concatenate two column vectors side by side, you need to always use ";". So, in your case, the correct syntax is:
PP_Tbl = [PP_Tbl; Y_sr; X_sp];
However, you must ensure that "Y_sr" and "X_sp" must be of the same length, which I do not think is the case here as the sizes of both "Y_sr" and "X_sp" are changing based on "j" in the for loop.
However, from my understanding, given that you will later use this for fitting via the "fitlm" function, why not simply take advantage of using the function like this:
mdl = fitlm(X,y);
So now you can fit the response "Y_sr" to Matrix of data "X_sp", since "Y_sr" are the same. You can simply worry about concatenating just "X_sp".
1 Comment
Stephen23
on 28 Apr 2017
Edited: Stephen23
on 28 Apr 2017
"In order to concatenate two column vectors side by side, you need to always use ";""
You might want to check this first in the documentation:
Or perhaps compare these in the command window:
>> colvec = randi(9,3,1);
>> [colvec;colvec]
>> [colvec,colvec]
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!