简体   繁体   English

如何从一个向量中删除一个向量的元素?

[英]How do I remove the elements of one vector from another?

I have a double value, A, which is 我有一个双精度值A,即

[1,4,7,6]

I also have B, which is an array that contains many more values. 我也有B,这是一个包含更多值的数组。 I have a new variable, C, which is essentially a double value of all these numbers (all of them in one cell, vs. five). 我有一个新的变量C,它实际上是所有这些数字的双精度值(它们全部集中在一个单元格中,而不是五个)。

[1,4,7,6]
[2,6,9,12]
[3,1,17,13]
[5,7,13,19]
[1,5,9,15]

How do I remove the elements (not the actual values) from C? 如何从C中删除元素(不是实际值)? I want to end up with this. 我想以此结束。

 [2,6,9,12,3,1,17,13,5,7,13,19,1,5,9,15]

How do I get this? 我怎么得到这个? I've used these commands: 我使用了以下命令:

C(A) = [];

and

C = C(setdiff(1:length(C),A));

The problem is that when I run that command, I get this instead of what I want. 问题是,当我运行该命令时,得到的不是我想要的。

[4,7,2,12,3,1,17,13,5,7,13,19,1,5,9,15]

Clearly that isn't the same as what I have. 显然,这与我所拥有的不一样。 It's throwing off the rest of my results and I need to fix this specific issue. 它影响了我的其余结果,我需要解决此特定问题。

Thanks in advance :) 提前致谢 :)

EDIT: 编辑:

So I figured out that it's spewing the CORRECT numbers out, just in the wrong order. 所以我发现它以错误的顺序喷出了正确的数字。 I have to sort it in order for it to work correctly. 我必须对其进行排序以使其正常工作。 This is a problem because it causes the next command to be non-functional because the ismember command has issues with the removal (I don't know why, I'm still working on it). 这是一个问题,因为它会导致下一个命令不起作用,因为ismember命令的删除存在问题(我不知道为什么,我仍在处理它)。

Double array case 双阵列盒

If B is a double array, you can use setdiff with 'rows' and 'stable' options, like so - 如果B是双setdiff数组,则可以将setdiff'rows''stable'选项一起使用,如下所示-

C = reshape(setdiff(B,A,'rows','stable').',1,[])

With ismember , you can perform the same operation, like so - 使用ismember ,您可以执行相同的操作,如下所示-

C = reshape(B(~ismember(B,A,'rows'),:).',1,[])

You can also use a bsxfun approach as suggested by @Amro - 您也可以使用bsxfun建议的bsxfun方法-

C = reshape(B(~all(bsxfun(@eq, B, A),2),:).',1,[])

Cell array case 单元阵列盒

If B is a cell array with number of elements in each cell equal to the number of elements in A , then you can firstly convert it to a double array - B = vertcat(B{:}) and then use either of the above mentioned tools. 如果B是一个单元格数组,每个单元格中的元素数等于A的元素数,则可以先将其转换为双B = vertcat(B{:})数组B = vertcat(B{:}) ,然后使用上述任何一个工具。

Or you can use a cellfun based approach that avoids conversion to a double array, like so - 或者,您可以使用基于cellfun的方法,避免转换为双cellfun数组,如下所示-

excl_rows = B(~cellfun(@(x1,x2) isequal(x1,x2), B, repmat({A},size(B,1),1)),:)
C = horzcat(excl_rows{:})

Or another cellfun based approach that avoids repmat - 或另cellfun基于cellfun的避免repmat的方法-

exclB = B(~cellfun(@(x1) isequal(x1,A), B),:)
C = horzcat(exclB{:})

Example with explanation - 带说明的示例-

%// Inputs
A = [1,4,7,6]
B = {[1,4,7,6]
    [2,6,9,12]
    [3,1,17,13]
    [5,7,13,19]
    [1,5,9,15]}

%// Compare each cell of B with A for equality.
%// The output must be a binary array where one would be for cells that have 
%// elements same as A and zero otherwise.
ind = cellfun(@(x1) isequal(x1,A), B) 

%// Thus, ~ind would be a binary array where one would reperesent unequal 
%// cells that are to be selected in B for the final output.
exclB = B(~ind)

%// exclB is still a cell array with the cells that are different from A.
%// So, concatenate the elements from exclB into a vector as requested.
C = horzcat(exclB{:})

Output - 输出-

A =
     1     4     7     6
B = 
    [1x4 double]
    [1x4 double]
    [1x4 double]
    [1x4 double]
    [1x4 double]
ind =
     1
     0
     0
     0
     0
exclB = 
    [1x4 double]
    [1x4 double]
    [1x4 double]
    [1x4 double]
C =
     2     6     9    12     3     1    17    13     5     7    13    19     1     5     9    15

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

相关问题 如何从一个向量中获得不在另一个向量中的元素 - How to get the elements from one vector that aren't in another vector 如何从另一个数组中删除一个数组的元素? - How to remove elements of one array from another? 如何在MATLAB中的向量中删除一组索引处的元素? - How do I remove elements at a set of indices in a vector in MATLAB? 如何在Matlab中找到矩阵中一个向量的元素的索引? - How do I find the indices of the elements of one vector in a matrix in Matlab? 我如何从两个向量中找到一个150 * 25的矩阵,使得每个向量元素与另一个暗1 * 150&1 * 25向量的元素相乘? - How do i find a matrix of 150*25 from two vectors such that each vector elements multiply with each element of another vector of dim 1*150 &1*25? 从矢量中删除单个元素 - Remove single elements from a vector 循环以删除向量的重复元素并添加另一个向量的对应元素 - loop to remove repeated elements of a vector and add corresponding elements of another vector 如何将向量中的元素与另一个向量进行比较 - How to compare the elements in a vector with another vector 如何从匹配另一个向量中值的矩阵中删除所有行? - How to remove all the rows from a matrix that match values in another vector? 计算向量中的值小于另一个向量中的每个元素 - Count values in a vector less than each one of the elements in another vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM