Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python合并函数翻译成Matlab不工作_Python_Matlab_Merge - Fatal编程技术网

Python合并函数翻译成Matlab不工作

Python合并函数翻译成Matlab不工作,python,matlab,merge,Python,Matlab,Merge,对于以下运行良好的Python代码 #-------------------------------------------------- s = [[1,2,3],[3,6],[9,0],[0,8]] s = [set(i) for i in s if i] t = [] while len(t) != len(s): t = s s = [] for i in range(len(t)): for j in range(len(s)):

对于以下运行良好的Python代码

#--------------------------------------------------
s = [[1,2,3],[3,6],[9,0],[0,8]]
s = [set(i) for i in s if i]
t = []
while len(t) != len(s):
    t = s
    s = []
    for i in range(len(t)):
        for j in range(len(s)):
            if not s[j].isdisjoint(t[i]):
                s[j] = s[j].union(t[i])
                break
        else: s.append(t[i])
print(s)
#--------------------------------------------------
>>> [{1, 2, 3, 6}, {0, 9, 8}]
#--------------------------------------------------
我对Matlab的翻译如下:

%--------------------------------------------------
s = {[1,2,3],[3,6],[9,0],[0,8]};
t = {};
while length(t) ~= length(s)
    t = s;
    s = {};
    for i=1:length(t)
        for j=1:length(s)
            if ~isempty(intersect(s{j},t{i}))
                s{j} = union(s{j},t{i});
                break
            else
                s = [s; t{i}];
            end
        end
        if isempty(s); s = [s; t{i}]; end
    end
end
s{:}
%--------------------------------------------------
ans =
     1     2     3     6
ans =
     0     8     9
ans =
     0     8
%--------------------------------------------------
工作不正确

问:这是什么原因?

参考文献:

Python代码之后

嗯,我可以找到如下解决方案

%--------------------------------------------------
s = {[1,2,3],[3,6],[9,0],[0,8]};
t = {};
while length(t) ~= length(s)
    t = s;
    s = {};
    for i=1:length(t)
        for j=1:length(s)
            if ~isempty(intersect(s{j},t{i}))
                s{j} = union(s{j},t{i});
                j = 0;
                break;
            end
        end
        if isempty(s) || (j == length(s));
            s = [s; t{i}];
        end
    end
end
s{:}
%--------------------------------------------------
ans =
     1     2     3     6
ans =
     0     8     9
其中
j=0
如果是空的(j==长度(s))满足
else:
与Python版本相同