Delete row from matrix given an id- Matlab

How can I delete a certain row from a matrix depending on the 1st column value?

For example: A=[1 2 3;3 4 5;5 6 7] where the values of the first column represent the ids and I want to delete the row that has 5 as an id. I already know that A(3,:)=[] deletes the third row, but what if I have the id and don't know the row number?


You can use find:

A =

 1     2     3
 3     4     5

Note that, as mentioned by Divakar, thanks to logical indexing you can even omit the find:



A(logical([0 0 1]),:)

are equivalent so




will give the same result.

If you have only element as id, then I would go with @yoh.lej's solution. But, if you happen to have an array of elements as id, you can use one of the approaches listed next.

Approach # 1 (With ismember)

A(ismember(A(:,1),ids),:) = [];

Approach # 2 (With bsxfun)

A(any(bsxfun(@eq,A(:,1),ids(:)'),2),:) = [];

If the first column of A has unique ids, then you have two more approaches to play with.

Approach # 3 (With intersect)

[~,remove_rowid] = intersect(A(:,1),ids);
A(remove_rowid,:) = [];

Approach # 4 (With setdiff)

[~,select_rowid] = setdiff(A(:,1),ids,'stable');
A = A(select_rowid,:);

Need Your Help

Synclock for creation of unique directories in multithreaded environment

c# multithreading parallel-processing locking

In my application I am creating reports. The reports are created by jobs that run in parallel. The reports need to be stored in unique directories.

How to set an Image on QLabel from other form in Qt

c++ qt

Before taking up the main subject, please mind that i`m a beginner of Qt.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.