Generate a matrix which contains 70% of the data from another matrix

4 views (last 30 days)
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.

Accepted Answer

Matt Fig
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
Ebrahim
Ebrahim on 5 Jun 2011
Hi Matt,
Thanks for that, I'm new to matlab so I don't know what ceil is but I figured out my own solution to it. This is what I did:
A =magic(5); % 5*5 matrix
n=round(length(A)*.7); % rounds a value of rows around 70%
B=(A(1:n,:)); % gets the output for n rows
So even if the number of rows is an odd number or if 70% of the number of rows returns a decimal number, I just round it up and use that.
Matt Fig
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)];

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!