在MATLAB中删除多维数组中一组索引处的元素? [英] Remove elements at a set of indices in a multidimensional array in MATLAB?
问题描述
我有一个包含1000个元素(1000x3)的多维数组A
.我有另一个向量,该向量具有要从此数组中删除的元素的索引位置.
I have a multidimensional array A
with 1000 elements (1000x3). I have another vector with index positions of elements I want to remove from this array.
我尝试使用此A(indices) = []
或A(indices,:,:) = []
,但是问题是结果更改了A
的尺寸,所以如果indices
有10个元素,我发现A
的大小变为2990x1
而不是990x3
.任何人都可以建议如何删除在A
中尺寸不变的A
中具有indices
的元素仍然是n x 3
?
I've tried using this A(indices) = []
or A(indices,:,:) = []
, but the problem is that the result changes A
's dimension, so if indices
has 10 elements, I find A
's size become 2990x1
instead of 990x3
. Anyone can advise how to remove the elements having the indices
in A
where A
's dimensions won't change will still be n x 3
?
推荐答案
您可以使用逻辑索引以过滤矩阵,例如
You can use logical indexing to filter the matrix, for example,
A=rand(1000,3);
A(A(:,1)>0.9)=[];
删除第一列中值大于0.9
的A
行.
which removes the rows of A
that have a value greater than 0.9
in the first column.
我不确定您原来的方法为什么不起作用.
I'm not sure why your original approach didn't work though.
这篇关于在MATLAB中删除多维数组中一组索引处的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!