Matlab中的数组运算

时间:2014-06-25 17:42:59

标签: arrays matlab vector matrix calculus

我想要一个函数来计算以下操作:enter image description here

我做了这个函数,它在输入端需要一个矩阵,并在另一个矩阵中返回它的每两行之间的距离。

RGB_dist_full定义:

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定义:

function[distance]=RGB_dist(x,y)

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

end

主程序如下:

    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);

为什么我会在使用*时出现错误 MTIMES并非完全支持 整数类。至少一个输入 必须是标量。 要计算元素TIMES,请使用 时间(。*)代替。'对于函数的第二次调用,而第一次调用返回所需的输出?

1 个答案:

答案 0 :(得分:1)

对于这些类型的计算,您希望转换为double精度,因为sqrt(integer)通常不是整数。只需double(rgbImage)读取图像即可。