Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting only a specific number of rows fulfilling a condition

I currently started to work with octave for some data analysis and have some problems for a specific matrix manipulation.

Assume you have the following data matrix:

      A =          1   11   22   33        44   13   12   33         1   14   33   44  

Now I would like to delete all rows of this matrix which don't accomplish e.g. the following condition.

      octave:6> A(:, 4) == 33     ans =         1        1        0  

And I'll get the matrix of this form which only selects these rows:

      A_new =          1   11   22   33        44   13   12   33  

I know this is possible with the help of some loops. But is there maybe a cleaner solution e.g. by using the provided standard library? That would be great :]

Some similar question was also already posted for R: In R, select rows of a matrix that meet a condition

like image 475
Ruun Avatar asked Nov 13 '11 13:11

Ruun


People also ask

How do you count the number of rows for the satisfying condition in pandas?

Using count() method in Python Pandas we can count the rows and columns. Count method requires axis information, axis=1 for column and axis=0 for row. To count the rows in Python Pandas type df. count(axis=1) , where df is the dataframe and axis=1 refers to column.

How do I select a row with a specific value in R?

By using bracket notation on R DataFrame (data.name) we can select rows by column value, by index, by name, by condition e.t.c. You can also use the R base function subset() to get the same results. Besides these, R also provides another function dplyr::filter() to get the rows from the DataFrame.


1 Answers

Try:

A = [     1   11   22   33     44  13   12   33     1   14   33   44 ]; idx = ( A(:,4)==33 ); A_new = A(idx,:) 

This is using logical indexing

like image 182
Amro Avatar answered Oct 06 '22 17:10

Amro