如何检查是否在一个阵列的任何变量是在另一个 [英] How to check if any variable in one array is in another

查看:161
本文介绍了如何检查是否在一个阵列的任何变量是在另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发用MATLAB程序,计算功率的数字,它们加在一起,然后看是否有任何第一组数字(数字到权力)的任何等于增加号码的权力。我想检查此为第一阵列中的每个值,但是,我得到这样的输出:

  M =
       1
     128
    2187
   16384
   78125
  279936
  823543
 2097152
 4782969
千万

每个 M 价值,这是一个简单的对数组的循环而造成的。所以,当我去检查,如果 M 是数组中,它会检查是 [1,128,2187,16384,78125 ...] 数组中,答案是否定的。我怎样才能得到它来评估每个条目,是这样的:

 阵n是[1,128,2187,16384]
为M = N
m = 1时
是M的阵列?没有
M = 128
是M的阵列?没有
M = 2187
是M的阵列?是
M = 16384
是M的阵列?没有
结束

我的code是如下:

  C = [];
D = [];
E = [];
F = [];
numbers1 = [];
numbers2 = [];数= 10;
权力= 10;对于i = 1:号码
    对于j = 3:权力
        C = [C; I ^ J]。
    结束
    C =转置(C);
    D = [D组; C];
    C = [];
结束[〜,B] =独特(D(:,1)); %指标为唯一值在D中的第一列
D(B,:);在这些行%值对于i = D
    对于= D
        E = [E;我+ A];
    结束
    E =转(E);
    F = [F,E]。
    E = [];
结束[〜,B] =唯一的(F(:,1)); %指标为唯一值的F第一列
F(B,:);在这些行%值为M = D%,这是循环上述
        米
结束


解决方案

例如向量:

 >> M = [1 3 5 9]。
N = [5 2 1 4 8]。


  1. 要检查是否向量 M 的每个元素是 N ,使用的 ismember

     >> ismember(M,N)
    ANS =
         1 0 1 0


  2. 要获取值,而不是指数:上使用逻辑索引 M

     >>米(ismember(M,N))
    ANS =
         1 5

    ,或直接使用 相交

     >>相交(M,N)
    ANS =
         1 5


I'm developing a program with MatLab that calculates powers of numbers, adds them together, and then sees if any of the first set of numbers (numbers to powers) equals any of the added numbers to powers. I'm trying to check this for each value in the first array, however, I am getting an output like this:

m =
       1
     128
    2187
   16384
   78125
  279936
  823543
 2097152
 4782969
10000000

for each m value, which is just the result of a simple for loop of the array. So when I go to check if m is in the array, it checks is [1, 128,2187,16384,78125...] in the array, and the answer is no. How can I get it to evaluate each individual entry, like this:

Array n is [1,128,2187,16384]
for m = n
m = 1
Is m in array? No
m = 128
Is m in array? No
m = 2187
Is m in array? Yes
m = 16384
Is m in array? No
end

My code is below:

C = [];
D = [];
E = [];
F = [];
numbers1 = [];
numbers2 = [];

numbers = 10;
powers = 10;

for i = 1:numbers 
    for j = 3:powers  
        C = [C;i^j]; 
    end  
    C = transpose(C);
    D = [D;C];  
    C = [];
end

[~,b] = unique(D(:,1)); % indices to unique values in first column of D
D(b,:);                  % values at these rows

for i = D
    for a = D
        E = [E;i+a];
    end
    E = transpose(E);
    F = [F;E];  
    E = [];
end

[~,b] = unique(F(:,1)); % indices to unique values in first column of F
F(b,:);                  % values at these rows

for m = D % this is the for loop mentioned above
        m
end

解决方案

Example vectors:

>> m = [1 3 5 9];
n = [5 2 1 4 8];

  1. To check if each element of vector m is in n, use ismember:

    >>ismember(m,n)
    ans =
         1     0     1     0
    

  2. To get the values, not the indices: use logical indexing on m:

    >> m(ismember(m,n))
    ans =
         1     5
    

    or directly use intersect:

    >> intersect(m,n)
    ans = 
         1     5
    

这篇关于如何检查是否在一个阵列的任何变量是在另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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