如果您可以按照 Luis 的建议在不同的向量中找到具有相应行号的列号,您也可以使用它 -
%// Main portion
haystack_t = haystack';
num1 = strfind(num2str(haystack_t(:))',num2str(needle(:))');
col = rem(num1,size(haystack,2));
ind = floor(num1/size(haystack,2))+1;
%// We need to remove indices that get into account because of concatenation of all the numbers into one big string
rm_ind = col> (size(haystack,2) - numel(needle))+1;
col(rm_ind)=[];
ind(rm_ind)=[];
运行各种针输入 -
RUN1 (Original values):
needle =
1 2 3
haystack =
0 0 1 2 3 0 1 2 3
0 1 2 3 0 1 2 3 0
0 0 0 1 2 3 0 0 0
col =
3 7 2 6 4
ind =
1 1 2 2 3
RUN2 :
needle =
1 2 3 0 1
haystack =
0 0 1 2 3 0 1 2 3
0 1 2 3 0 1 2 3 0
0 0 0 1 2 3 0 0 0
col =
3 2
ind =
1 2