Generate a matrix which contains 70% of the data from another matrix
4 views (last 30 days)
Show older comments
I am trying to define a matrix to be used as training data to be a subset (70%) of another matrix (100%). The remaining 30% will be used to validate the model that I will apply to the 70%. I am writing a script to do this automatically.
So basically say I have a 100 rows, I want to generate a matrix with 70 rows, and this could be from row 1 to row 70 and contain all the columns. However I don't know the amount of rows in each matrix because I have different sets of data to apply my script to.
What would be a way to go about this? Any help is appreciated.
0 Comments
Accepted Answer
Matt Fig
on 5 Jun 2011
When the number of rows is known (and 'nice' for 70%):
A = magic(10); % Lets use 10 as an example
B = A(1:7,:); % Take the first 7 rows of A and all columns.
If the number or rows of A is unknown:
A = magic(ceil(rand*20)); % A of unknown row size.
B = A(1:ceil(.7*size(A,1)),:) % About 70% of the rows of A.
2 Comments
Matt Fig
on 5 Jun 2011
CEIL just rounds up. You can always check the help when you find a function you haven't seen before:
>> help ceil
You will want to be careful with the solution you are using. LENGTH returns the largest length of its argument, so if your array has more columns than rows, you will get an error. For example, try it with this:
A = [magic(5),magic(5)];
More Answers (0)
See Also
Categories
Find more on Logical 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!