MATLAB Answers

# Divide the matrix randomly, based on columns

1 view (last 30 days)
Naina on 11 Dec 2019
Commented: Rik on 11 Dec 2019
Hi
I have a matrix x of size 78000x204. I need to divide it into nonoverlapping but random 70:15:15 percent of the data, based on number of columns.
Any help, how to do that.?

#### 0 Comments

Sign in to comment.

### Accepted Answer

Rik on 11 Dec 2019
You mean like this?
fractions=[70 15 15];
x=rand(78000,100);%use 100 to show the split is indeed 75:15:15
groups=cell(size(fractions));%pre-allocate output
f=fractions/sum(fractions);
f=[0 cumsum(f)];
r=randperm(size(x,2))/size(x,2);
for n=1:numel(groups)
L= r>f(n) & r<=f(n+1);
groups{n}=x(:,L);
end

#### 2 Comments

Naina on 11 Dec 2019
Yes, but i'm not sure if it's dividing the data randomly and nonoverlapping.?
I can't figure it out as it's a long data with 78000x204.
Rik on 11 Dec 2019
Because randperm is generating the indices, this is guaranteed not to result in overlapping groups. Unless there are columns in your data that are equal to eachother, there is not be any match between the groups.

Sign in to comment.

### More Answers (0)

Sign in to answer this question.