在matlab中删除矩阵行 [英] remove rows of matrix in matlab

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

问题描述

如果我有一个只有一列的矩阵X,并且在某些行中有一些负值.如何只删除负值?

If I have a matrix X with only one column and it has some negative values in some rows. How can I remove only the negative values?

示例:

X=[-1; 2; 3; -4; 5]

应成为:

X=[2; 3; 5]

此外,如何从

y=[1; 2; 3; 4; 5]

基于在X中找到负值的位置?此操作后,y应该为[2; 3; 5].

based on where the negative values in X are found? y should be [2; 3; 5] after this operation.

推荐答案

X中删除负值:

您可以将X重新分配给仅包含X值且不为负的向量:

You can either reassign X to a vector which only contains the values of X which are not negative:

>> X = X(X>=0)
X =
     2
     3
     5

或从X中删除负值:

>> X(X<0) = []
X =
     2
     3
     5

基于X中负值的索引从y中删除值的过程类似.要么重新分配:

Removing values from y based on the indices of negative values in X is similar. Either reassign:

>> y = y(X>=0)
y =
     2
     3
     5

或删除:

>> y(X<0) = []      
y =
     2
     3
     5

如果要基于X中的负值修改两个向量,请记住首先对y进行操作,或者将逻辑向量存储在X<0的位置.例如:

If you want to modify both vectors based on the negative values in X remember to do the operation to y first or store a logical vector for the positions where X<0. For example:

>> ind = X < 0;
>> X(ind) = []
X =
     2
     3
     5
>> y(ind) = []
y =
     2
     3
     5

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

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