简体   繁体   English

在matlab中删除矩阵行

[英]remove rows of matrix in matlab

If I have a matrix X with only one column and it has some negative values in some rows. 如果我有一个只有一列的矩阵X ,并且在某些行中有一些负值。 How can I remove only the negative values? 如何仅删除负值?

Example: 例:

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

should become: 应该变成:

X=[2; 3; 5]

Also, how can I remove values from 另外,如何从中删除值

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

based on where the negative values in X are found? 基于X中的负值的位置? y should be [2; 3; 5] y应该是[2; 3; 5] [2; 3; 5] [2; 3; 5] after this operation. [2; 3; 5]

Removing negative values from X : X移除负值:

You can either reassign X to a vector which only contains the values of X which are not negative: 您可以将X重新分配给一个仅包含X值不为负的向量:

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

or delete the negative values from X : 或从X删除负值:

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

Removing values from y based on the indices of negative values in X is similar. 基于X中负值的索引从y删除值的过程类似。 Either reassign: 要么重新分配:

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

Or delete: 或删除:

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

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 . 如果要基于X的负值来修改两个向量,请记住首先进行y运算或为X<0的位置存储逻辑向量。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM