![]() |
比较MATLAB中两列的内容
我需要在MATLAB中比较两个表的内容,更确切地说是两列(每个表一个列),以查看第一列的每个元素(如果第二列中的元素相等)。
我应该使用for循环还是执行此操作的现有MATLAB函数? [B]回答:[/B] 如果顺序很重要,则进行[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/logicaloperatorselementwise.html"]逐元素比较[/URL] ,然后使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/all.html"]全部[/URL] %# create two arrays A = [1,2;1,3;2,5;3,3]; B = [2,2;1,3;1,5;3,3]; %# compare the second column of A and B, and check if the comparison is `true` for all elements all(A(:,2)==B(:,2)) ans = 1 如果顺序不重要并且所有元素都是唯一的,请使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/ismember.html"]ismember[/URL] all(ismember(A(:,1),B(:,1)) ans = 1 如果顺序不重要,并且有重复,请使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html"]sort[/URL] all(sort(A(:,1))==sort(B(:,2))) ans = 0 [url=https://stackoverflow.com/questions/2797532]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 01:04。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.