Problem with matrix manipulation- how can I avoid having to type in everything?

1 view (last 30 days)
I am looking to extract data from matrices, training (1500 by 3) and testing (500 by 3), n is the number before the colon. this is what I have by far;
% for n = 1
newTrainInput = training(1:1500,2);
newTrainOutput = training(1:1500,3);
newTestInput = testing(1:500,2);
newTestOutput = testing(1:500,3);
%for n=2
newTrainInput = [training(2:1500,2),training(1:1499,2)];
newTrainOutput = training(2:1500,3);
newTestInput = [testing(2:500,2),testing(1:499,2)];
newTestOutput = testing(2:500,3);
%for n=3
newTrainInput = [training(3:1500,2),training(2:1499,2),training(1:1498,2)];
newTrainOutput = training(3:1500,3);
newTestInput = [testing(3:500,2),testing(2:499,2),testing(1:498,2)];
newTestOutput = testing(3:500,3);
%for n=4
newTrainInput = [training(4:1500,2),training(3:1499,2),training(2:1498,2),training(1:1497,2)];
newTrainOutput = training(4:1500,3);
newTestInput = [testing(4:500,2),testing(3:499,2),testing(2:498,2),testing(1:497,2)];
newTestOutput = testing(4:500,3);
%for n=5
newTrainInput = [training(5:1500,2),training(4:1499,2),training(3:1498,2),training(2:1497,2),training(1:1496,2)];
newTrainOutput = training(5:1500,3);
newTestInput = [testing(5:500,2),testing(4:499,2),testing(3:498,2),testing(2:497,2),testing(1:496,2)];
newTestOutput = testing(5:500,3);
-
-
-
%for n=10
newTrainInput = [training(10:1500,2),training(9:1499,2),training(8:1498,2),training(7:1497,2),training(6:1496,2),training(5:1495,2),training(4:1494,2),training(3:1493,2),training(2:1492,2),training(1:1491,2)];
newTrainOutput = training(10:1500,3);
newTestInput = [testing(10:500,2),testing(9:499,2),testing(8:498,2),testing(7:497,2),testing(6:496,2),testing(5:495,2),testing(4:494,2),testing(3:493,2),testing(2:492,2),testing(1:491,2)];
newTestOutput = testing(10:500,3);

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 Mar 2015
Jessie - I think tha thou can simplify the above to something like the following
for u=1:n
newTrainInput = [];
newTestInput = [];
for v=u:-1:1
newTrainInput = [newTrainInput , training(v:1500-(u-v),2)];
newTestInput = [newTestInput , testing(v:500-(u-v),2)];
end
newTrainOutput = training(u:1500,3);
newTestOutput = testing(u:500,3);
% do something with the arrays
end
In your example, n would be 10 and we would then have an inner loop where we would append data to the newTrainInput and newTestInput as per your above description. I haven't tested this, but think that it will provide a start in simplifying your code.
  4 Comments
Geoff Hayes
Geoff Hayes on 15 Mar 2015
Jessie's answer moved here
I got an error in line 9 : newTrainInput(:,atBlock:atBlock+1) = training(v:1500-(u-v),2); I used the code;
for u=1:n
newTrainInput = zeros(1500-(u-1),2*(u-1));
newTestInput = zeros(500-(u-1),2*(u-1));
atBlock = 1;
for v=u:-1:1
newTrainInput(:,atBlock:atBlock+1) = training(v:1500-(u-v),2);
atBlock = atBlock + 2;
newTestInput(:,atBlock:atBlock+1) = testing(v:500-(u-v),2);
atBlock = atBlock + 2;
end
newTrainOutput = training(u:1500,3);
newTestOutput = testing(u:500,3);
% do something with the arrays
end
Geoff Hayes
Geoff Hayes on 15 Mar 2015
Jessie - please include the error message. As well, try stepping through the code with the debugger to see what is happening.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!