0

I have the sparse Matrix having 300 to 900 rows with 3 columns, I want the sampling of this matrix i.e 20 samples of Matrix of the whole Matrix. How can I sample my matrix MAT in Matlab.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147

2 Answers2

2

I understand your question as followed:
You have a matrix with a size of e.g. 900x3 and you want to have a matrix which contains only the rows 400 to 500.
.

If this is what you are looking for, the code is

new_Mat = Mat(400:500,:)

This returns a new Matrix (new_Mat) containing the rows 400 to 500 and all columns. If you use e.g:

new_Mat = Mat(300:500,1:2)

it would return the first 2 columns of the rows 300 to 500.

For your problem of wanting the xth element you can just use the coordinates. Either you can adress the 40th row and 2 column over

Mat(40,2);

Or you use a one dimensional adress.

Mat(80); 

adresses the 80th element, but careful he is counting first the rows then the columns. So it would be row 80, column 1. If you don't want to use fix values you can use the return values (1 dimensional, or 2 dim) of functions or looping parameters for addressing your element.

The Minion
  • 1,164
  • 7
  • 16
  • My question isn't, I have to pick `20` values after `20` values. –  May 14 '14 at 06:42
  • so you have a set of data placed into a 900x3 matrix. And you want the first ,21th, 41th and so on row? Or do you want the 1st, 21th, 41th and so on element? Could you post an example in your question. For example a tiny matrix and which elements you want? – The Minion May 14 '14 at 06:44
  • Yeah u r understanding right, I want 21th 41th and so on values, but the difference of 20 sholoud be random, i.e may b 30 or 40 etc –  May 14 '14 at 06:50
  • i added new lines to my answer hopefully this explains your problem. – The Minion May 14 '14 at 06:56
2

I assume you want random sampling (without replacement); that is, you want to pick n elements out of matrix A randomly. For that you can apply randsample on the linearized, full version of A:

result = randsample(full(A(:)), n);

If you want to avoid converting A into full (for example, because of memory limitations), use

result = A(randsample(numel(A), n)); %// result in sparse form

or

result = full(A(randsample(numel(A), n))); %// result in full form
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • `sparse` function can't use here? –  May 14 '14 at 10:32
  • 1
    Depending on which of the above lines you use, you will obtain the result in full or sparse form. To obtain it in sparse form, use `result = A(randsample(numel(A), n)); ` – Luis Mendo May 14 '14 at 10:41