-1

I have matrix

A = [1 2;2 2;3 3;4 3;5 3;6 3;7 2;8 3;9 2;10 2;11 3;12 3;13 2;14 2;15 3]

I need to randomly remove 10 row from A.
I am using with this code:

for i = 1:10
    x = randi([2  3],1);
    A(any(A==x,2),:)=[];
end

so A remaining five rows only. thank you so much..

Shai
  • 111,146
  • 38
  • 238
  • 371
8727
  • 73
  • 8

1 Answers1

2

If you want to randomly remove 10 rows out of 15, you baiscally need to randsample the rows to remove:

num_to_remove = 10;
idx = randsameple( size(A,1), num_to_remove );
A(idx,:) = [];  % remove the sampled rows

You only need to make sure size(A,1) >= num_to_remove.


PS,
It is best not to use i as a variable name in Matlab.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    I think that `randsample` may be a bit overkill here. `randsample` is from the Statistics and Machine Learning toolbox and has several options that aren't being used here. A call to [`randperm`](https://www.mathworks.com/help/matlab/ref/randperm.html) would accomplish the same thing and comes with vanilla MATLAB. – craigim Nov 02 '16 at 13:38