Matlab-仅返回不包含矩阵B的某些值的矩阵A的行 [英] Matlab - Return only the rows of a matrix 'A' that not contain some values of matrix 'B'

查看:357
本文介绍了Matlab-仅返回不包含矩阵B的某些值的矩阵A的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅返回不包含某些值的矩阵'A'的行(这些值是数组'B')?

How do I return only the rows of a matrix 'A' that not contain some values (These values ​​are an array 'B')?

  A = {'A1',  5  'P01,P02,P03,P04,P07'; 
        'A2'  7,  'P07,P10';
        'A3'  8,  'P07,P09';
        'A4'  8,  'P10,P11'};

    B = { 'P07'; 'P10'; 'P11'};

我只需要返回:

'A1'  ( P01,P02,P03,P04 not exist in B)
'A3'  (P09 not exist in B)

预先感谢您的帮助

推荐答案

由于您正在处理形状怪异的单元格数组和一些奇怪的字符串操作,因此,我不知道如何用一个语句来彻底解决这个问题.您可以尝试以下循环:

Since you are dealing with weirdly shaped cell arrays and some strange string operations, I do not know how to solve this cleanly with one statement. You can try the following loop:

R = {};
for i = 1 : size(A, 1)
    test = strsplit(A{i, 3}, ',');
    for j = 1 : length(test)
        if nnz(strcmp(B, test{j})) == 0
            R = [R; A(i, 1)];
            break;
        end
    end
end

结果是:

R = 

    'A1'
    'A3'

如果可以只使用每个搜索字符串的数字分量而不是字符串的单元格数组,那么可以更快地进行这些计算.

Of course these calculations could be made much faster if it was possible to work with just the numerical components of each search string in an actual matrix rather than cell arrays of strings.

这篇关于Matlab-仅返回不包含矩阵B的某些值的矩阵A的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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