How to check if any variable in one array is in another

前端 未结 1 1520
陌清茗
陌清茗 2021-01-24 14:04

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 t

相关标签:
1条回答
  • 2021-01-24 14:34

    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
      
    0 讨论(0)
提交回复
热议问题