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

 采纳的回答

Try this:
X=[x1 x2 x3]; %25by3 matrix
k = randperm(size(X,1));
Ex_Ran = X;
Ex_Ran(k(1:10),:) = [] %extract 10 rows randomly

7 个评论

Your code exracts 10 rows and gives me back the remaining 15. The thing is that i want to know also which 10 rows have been extracted and the 15 which are the remaining ones.
This gives you both:
Ex_Rem = X;
Ex_Rem(k(1:10),:) = []; % Remaining Rows
Ex_Ext = X(k(1:10),:); % Extracted Rows
The row numbers of the extracted rows are stored in k(1:10), and the remaining row numbers are stored in k(11:end)
Note that instead of:
k = randperm(somenumber);
k2 = k(1:n); %only use the first n numbers of k
You can use:
k2 = randperm(somenumber, n); %return n integers picked from 1:somenumber
Originally randperm() did not allow a second argument; it was added a couple of years back. Yesterday a poster ran into the problem of having a release too old to support it.
In the case where, such as here, one wants to also know what was not selected, then the single-argument randperm() is better.
Hello,
I want to tell you that I have the following issue: I am trying to remove 2000 random columns from a matrix A (1653X8000). I applied the above code but Matlab gives me the following error:
Index exceeds the number of array elements (1653)
The code I wrote:
A=[x1 x2, x3.....] %1653by8000 matrix
k=randperm(size(A,1));
B=A;
B(:,k(1:2000))=[];
Your help is valuable!!
k is a reordering of the numbers between 1 and the number of rows in A.
You want a reordering of the numbers between 1 and the number of columns in A (which is the size of A in the second dimension) if you want to use it as a set of indices into the columns of B (which starts off as a copy of A.)

请先登录,再进行评论。

更多回答(3 个)

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)),:);
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 个评论

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.

请先登录,再进行评论。

类别

帮助中心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!

Translated by