Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Time complexity of incrementing a value in a matrix in my code is awful.

1 回表示 (過去 30 日間)
Chris Garry
Chris Garry 2013 年 10 月 21 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I'm writing a function that takes in two images, looks at respective pixel pairs between the images (e.g. [image_A(1,1),image_B(1,1)], [image_A(1,2),image_B(1,2)]), defines a binning scheme for these joint values, and returns a matrix that indicates how many pixel pairs fell into the bins. I'm doing this using histc to determine which bin each pixel falls into independent of the other, then updating the bin matrix considering the two values jointly as I don't know if I can do this with histc on two values. The code is taking forever to execute and it appears to be this one simple line indicated by the MATLAB profiler. See code below:
function [joint_dist] = jointDistFromImages(image_A, image_B, num_bins)
%%Map joint pixel values for image_A and image_B into an n*n bin matrix
num_pixels = numel(image_A);
image_linear_A = reshape(image_A,1,num_pixels);
image_linear_B = reshape(image_B,1,num_pixels);
joint_dist = zeros(num_bins, num_bins);
increment = (1/num_pixels);
for i=1:num_pixels
temp1 = histc(image_linear_A(i),0:(256/num_bins):256);
temp1 = temp1(1:1:end-1);
temp2 = histc(image_linear_B(i),0:(256/num_bins):256);
temp2 = temp2(1:1:end-1);
row = find(temp1==1);
col = find(temp2==1);
joint_dist(row,col) = joint_dist(row,col) + increment;
end
end
Here is the output of the MATLAB profiler:
%

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 10 月 21 日

この質問は閉じられています。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by