Merge multi-row values to single row in table for classification training

1 view (last 30 days)
Hi, i am stuck in preparing data for training. i have a table like
P Q
5 4
3 3
2 3
1 4
1 5
...
i wish to create another table from it like:
P(t-5) Q(t-5) P(t-5) Q(t-5) P(t-5) Q(t-5) P(t-5) Q(t-5) P(t) Q(t)
0 0 0 0 0 0 0 0 5 4
0 0 0 0 0 0 5 4 3 3
0 0 0 0 5 4 3 3 2 3
0 0 5 4 3 3 2 3 1 4
5 4 3 3 2 3 1 4 1 5
...
For a big dataset, i wonder except for for-loop, if there is any other ways to compute it? i actually want to feed this small section(curve) data to train my classification. i wonder if this is a good idea.

Answers (1)

Eric Tao
Eric Tao on 9 Feb 2018
Assume your original table is called 'data', and results will be stored in a new table called 'new', then run:
new = table();
new.P_minus4 = [zeros(4,1);data.P(1:end-4)];
new.Q_minus4 = [zeros(4,1);data.Q(1:end-4)];
new.P_minus3 = [zeros(3,1);data.P(1:end-3)];
new.Q_minus3 = [zeros(3,1);data.Q(1:end-3)];
new.P_minus2 = [zeros(2,1);data.P(1:end-2)];
new.Q_minus2 = [zeros(2,1);data.Q(1:end-2)];
new.P_minus1 = [zeros(1,1);data.P(1:end-1)];
new.Q_minus1 = [zeros(1,1);data.Q(1:end-1)];
new.P = data.P;
new.Q = data.Q;
And you will get what you want. If you want the code to be more concise, run this lines:
new = table();
for i = 4:-1:1
m = num2str(i);
eval(['new.P_minus',m,' = [zeros(',m,',1);data.P(1:end-',m,')];']);
eval(['new.Q_minus',m,' = [zeros(',m,',1);data.Q(1:end-',m,')];']);
end
new.P = data.P;
new.Q = data.Q;

Categories

Find more on Statistics and Machine Learning Toolbox 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!