Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top