how to split matrix by percentage

7 views (last 30 days)
babis
babis on 2 Dec 2013
I have a matrix R(u,v) and i want to split into 2 new matrices M, N. M will be the first 10% of the rows of R and N will be the rest 90% of the rows
  2 Comments
babis
babis on 3 Dec 2013
thanks for the answers. you helped a lot. I tried this:
a = x(1:end*0.1, 1:end);
b = x(end*0.1:end, 1:end);
c = x(1:end, 1:end*0.1);
d = x(1:end, end*0.1:end);
it seems that it works well.
Image Analyst
Image Analyst on 3 Dec 2013
That's what I said, except that my code was robust and correct while yours was not. What if end*0.1 is a fractional number? It will fail whereas I cast to integer so my code won't fail. My code gives N and M like you asked for whereas yours doesn't, and yours doesn't give the final 90% where if you do b(end*.1:end) you'll run into the same problem of fractional indexes. I'd recommend you use my more robust code.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 3 Dec 2013
Try this:
% Create some sample data 100 rows by 101 columns.
R = rand(100, 11);
% Determine how many rows 10% is.
[rows, columns] = size(R);
% Determine the last row number of the top (upper) 10% of rows.
lastRow = int32(floor(0.1 * rows));
% Get first 10% into one array M:
M = R(1:lastRow, :);
% Get the rest into one array N:
N = R(lastRow+1:end, :);
  9 Comments
Image Analyst
Image Analyst on 5 Jul 2021
Not sure I could help. You can try pca to find out how much of the variation is explained by each PC. I think there is also a stepwise regression in MATLAB but I don't know the details. I suggest you explain it thoroughly in a new question, not here in @babis's 8 year old question. Explain it well and attach your data and code and someone may be familiar with the process you should use.
Cappriece Clarke
Cappriece Clarke on 7 Jul 2021
Good day, I totally understand. Thank you.

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 2 Dec 2013
Edited: Azzi Abdelmalek on 2 Dec 2013
%----Example-------
n=randi(100);
m=10;
R=rand(n,m)
%---------------------
n1=ceil(0.1*n)

Community Treasure Hunt

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

Start Hunting!