在matlab中删除矩阵行 [英] remove rows of matrix in 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屋!