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

percentagesubset

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.

Best Answer

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.