Selecting a subset of a larger matrix with criteria

1 Ansicht (letzte 30 Tage)
Noah
Noah am 2 Jul. 2013
Hi,
I have a matrix that is Nrows x 150000 that includes a small number values that are less than or equal to -1. I would like to create a smaller Nrows x 100000 matrix that only includes values greater than -1 using a random subset of the remaining values in the larger matrix. It could also be the first 100000 values in each row that fit the criteria, but it cannot be, for example, the largest 100000 values, as this would introduce bias. Therefore, I have not been able to figure this it out by sorting the values. It is also complicated by the fact that each row has a different number of values that are <=-1, so I can't simply drop those values; it would create a matrix with different numbers of columns.
Please help! And thank you in advance. Noah

Akzeptierte Antwort

Hugo
Hugo am 2 Jul. 2013
Dear Noah,
Suppose that A is your original matrix and B is the matrix you want to obtain. You could do something like this:
B=zeros(N,100000);
for ind1=1:N
u=A(ind1,A(ind1,:)>-1);
B(ind1,:)=u(randperm(length(u),100000));
end
The code chooses the elements randomly. The code is assuming that there are always more than 100000 values greater than -1. Otherwise, you will get an error in the fourth line.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by