您在内部循环的第一次迭代中覆盖 attributes:
例子:
data{1} = 'aabbcc';
data{2} = 'bbaacc';
data{3} = 'ccaabb';
attributes{1} = 'aa';
attributes{2} = 'bb';
attributes{3} = 'cc';
for i = 1:length(attributes)
for j = 1:length(data)
attributes{i} = strfind(data{j}, attributes{i});
end
end
内部循环第一次迭代后j = 1:
attributes{1} = 1
内部循环第一次迭代后j = 2:
attributes{1} = []
因为 attributes{1} = 1 来自上一次迭代。
attributes{i} = strfind(data{j}, attributes{i}); 相当于:
attributes{1} = strfind(data{2}, 1);
并且在数据中找不到 1{2}。
您可能的意思是,类似于以下代码:
data{1} = 'aabbcc';
data{2} = 'bbaacc';
data{3} = 'ccaabb';
attributes{1} = 'aa';
attributes{2} = 'bb';
attributes{3} = 'cc';
%Initialize new 2D cell array.
attrib_indexes = cell(length(attributes), length(data));
for i = 1:length(attributes)
for j = 1:length(data)
%Store result in attrib_indexes{i, j} instead of attributes.
attrib_indexes{i, j} = strfind(data{j}, attributes{i});
end
end
我建议你学习使用调试器,它比在 Stack Overflow 中发布问题更容易......