I have a 25by3 matrix and i want to remove randomly 10 rows (I have already completed this part). The problem is that i want the original matrix which is 25by3 to be 15by3 after removing 10 rows. Thats my question
显示 更早的评论
Thats my code
X=[x1 x2 x3]; %25by3 matrix
%
k = randperm(size(X,1));
Ex_Ran= X(k(1:10),:)%extract 10 rows randomly
采纳的回答
更多回答(3 个)
Andrei Bobrov
2015-5-18
n = 10;
m = size(X,1);
out = X(rendperm(m,m-n),:);
Mark Stone
2015-5-18
编辑:Mark Stone
2015-5-18
I show you this just so that you 'll be aware of the very handy setdiff function. it might not be as fast executing as the previous answer, but it is elegant.
X =[x1 x2 x3]; %25by3 matrix
k = randperm(size(X,1));
X = X(setdiff(1:25,k(1:10)),:);
Or you can make the left hand side a new variable, such as Y, if you want to preserve the original 25 by 3 matrix as is.
Y = X(setdiff(1:25,k(1:10)),:);
Murali Krishna
2015-5-18
编辑:Murali Krishna
2015-5-18
0 个投票
To extract 10 rows of a given matrix
a=rand(25,3);%25*3 matrix
b=randperm(10);
c=a([b],:);
k=setdiff(a,c,'rows');
2 个评论
Walter Roberson
2015-5-18
That is going to extract the first 10 rows, but in a random order. You need to randperm() over the whole 25 and take only 10 of the scrambled values as the indices to extract.
Murali Krishna
2015-5-18
Thank u...
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!