在matlab中矢量化嵌套for循环

时间:2011-12-11 11:34:32

标签: matlab vectorization

我们如何在matlab中对这类代码进行矢量化?

for i = 1:N
for k = 1:64
    if (pixels(i,k)==1)
     p(character(i),k)= p(character(i),k)+1;
    end
end    
end

1 个答案:

答案 0 :(得分:1)

以下内容应相同:

[i,j] = find(pixels(i,k) == 1);
if ~isempty(i)
    ind = sub2ind(size(p), character(i), j);
    %   or, equivalently:
    % ind = character(i) + (j-1) * size(p,1);
    p(ind) = p(ind) + 1;
end