给定一个ID从矩阵中删除行-Matlab [英] Delete row from matrix given an id- Matlab

查看:86
本文介绍了给定一个ID从矩阵中删除行-Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据第一列的值从矩阵中删除特定行?

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

例如:A=[1 2 3;3 4 5;5 6 7] 其中第一列的值代表ID,我想删除ID为5的行. 我已经知道A(3,:)=[]会删除第三行,但是如果我有ID并且不知道行号怎么办?

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?

推荐答案

您可以使用find:

id=5;
A(find(A(:,1)==id),:)=[]
A =

 1     2     3
 3     4     5

请注意,正如 Divakar 所述,由于逻辑索引,您甚至可以省略find:

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

A(3,:)

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

等价于此

A(find(A(:,1)==id),:)=[]

A(A(:,1)==id,:)=[]

将给出相同的结果.

这篇关于给定一个ID从矩阵中删除行-Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆