0

I would like a function to compute the following operation: enter image description here

I made this function that requires a matrix at its input, and returns the distances between every two lines of it in another matrix.

RGB_dist_full definition:

function[D]=RGB_dist_full(x)

I = nchoosek(1:size(x,1),2);

D = RGB_dist(x(I(:,1),:), x(I(:,2),:));
squareform(D)

end

RGB_dist definition:

function[distance]=RGB_dist(x,y)

  distance=sqrt(sum((x-y).^2*[3;4;2],2));

end

Main program looks like this:

    clc
    clear all

    rgbImage = imread('peppers.png');
    K=6;
    N=uint64(K*2); 

    rgb_columns = reshape(rgbImage, [], 3); 
    [unique_colors, m, n] = unique(rgb_columns, 'rows','stable'); 
    color_counts = accumarray(n, 1);

    [max_count, idx] = max(color_counts);

    Imgsize=size(rgbImage);

    U=unique_colors(1:N,:)
    size(U)

x=[62,29,64;
    63,31,62;
    65,29,60;
    63,29,62;
    63,31,62;];

   RGB_dist_full(x);
   RGB_dist_full(U);

Why do I get 'Error using * MTIMES is not fully supported for integer classes. At least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead.' for the second call of the function, whereas the first one returns the desired output?

Tanatos Daniel
  • 558
  • 2
  • 9
  • 27
  • 1
    well why would you want to keep this integer to begin with (sqrt(integer) is usually not an intager) ? just `double(rgbImage)` and carry on... – bla Jun 25 '14 at 17:55
  • @natan Indeed, that solved the problem. – Tanatos Daniel Jun 25 '14 at 18:56
  • I'll add this as an answer then... – bla Jun 25 '14 at 21:18
  • @TanatosDaniel I did tell you to do this in the previous question you asked regarding this same procedure and you accepted my answer.... Curious as to why you're asking this again. http://stackoverflow.com/questions/24398054/calculate-euclidean-distance-between-vectors-in-a-large-matrix-matlab/24398895#24398895 – rayryeng Jun 26 '14 at 00:39
  • Oh. You did. I'm sorry. I read your post in a hurry, seemed like the solutions from here [link](http://stackoverflow.com/questions/24404963/operations-with-arrays-in-matlab) and that's I marked it is an answer. I should've read it entirely. I'll be more careful from now on. Thanks! – Tanatos Daniel Jun 26 '14 at 09:28

1 Answers1

1

For these types of calculations you want to cast to double precision , because sqrt(integer) is usually not an integer. For that just double(rgbImage) immediately after reading the image will do.

bla
  • 25,846
  • 10
  • 70
  • 101