Matlab - 没有得到预期的结果 [英] Matlab - Not getting the expected result
问题描述
- 输入两个集合
- 使用
pdist2
取两个集合之间的距离,其中代码显示为这里。 在开始的时候会在两组之间取距离。然后,对于 second 集合,在每次迭代时,它将(i,j)位置设置为0
。当进入下一次迭代时,它应该将下一个位置值更改为0,同时将之前设置为0的值返回到其原始值。 - 请注意,
pdist2
的结果最初会以矩阵的形式返回,但是为了比较,我总结了矩阵的值比较。
基于此,我写了下面的函数(注意可以使用
pdist2。 m
功能来自函数m = pixel_minimize_distance(x = svn81& r = 81rel =nofollow> here ) ,y)
sum1 = 0;
sum2 = 0;
[r c] = size(y);
d1 = pdist2(x,y);
[r1 c1] = size(d1);
for i = 1:r1
for j = 1:c1
sum1 = sum1 + d1(i,j);
end
end
maximum = sum1;
for i = 1:r
for j = 1:c
o = y(i,j)
y(i,j)= 0;
d2 = pdist2(x,y);
[r2 c2] = size(d2);
for i = 1:r2
for j = 1:c2
sum2 = sum2 + d2(i,j);
end
end
$ b如果sum2> =最大值
如果o〜= 0
maximum = sum2;
m = o;
end
end
如果sum2 <=最大值
maximum = maximum;
end
y(i,j)= o;
end
end
end
现在,作为测试运行:
>> A = [1 2 3; 6 5 4];
>> B = [4 5 3; 7 8 1];
>> pixel_minimize_distance(A,B)
o =
4
o =
4
o =
1
o =
7
o =
7
o =
0
ans =
7
请看这里的答案是
7
(向下滚动,如果你看不见),而我手动计算这个期望值应该是3
,因为当我们将它设置为0
距离的总和将为142
。
任何想法在代码中可能是错的?我想应该是在设置
o = y(i,j)
的代码中的位置,其中o
表示原始值 ,但真的无法解决这个问题。
谢谢。
解决方案我认为你的代码中有很多冗余的命令。我只是删除他们,没有别的。我将
m
作为3
的值。我使用MATLAB的pdist2
函数和欧氏距离的平方(因为这是您提供的函数的默认值)。我没有得到142的距离。
以下是代码:
function m = pixel_minimize_distance(x,y)
[rc] = size(y);
maximum =(sum(sum(pdist2(x,y))))。^ 2; %下面解释
为i = 1:r
为j = 1:c
o = y(i,j);
y(i,j)= 0
sum2 =(sum(sum(pdist2(x,y))))^ 2;
如果sum2> =最大
如果o〜= 0
maximum = sum2;
m = o;
end
end
y(i,j)= o;
end
end
end
输出结果为: p>
y =
0 5 3
7 8 1
4 0 3
7 8 1
y =
4 5 0
7 8 1
$ =
4 5 3
0 8 1
code>y =
4 5 3
7 0 1
y =
4 5 3
7 8 0
m =
3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%
说明:
您已经写下了以下代码片段:
d2 = pdist2(x,y);
[r2 c2] = size(d2);
for i = 1:r2
for j = 1:c2
sum2 = sum2 + d2(i,j);
end
end
这只是计算两组之间的距离使用
pdist2
并总结整个距离矩阵,得到一个存储在sum2
中的值。
让我们来看看我的代码:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ sum $(sum(sum(pdist2(x,y))) 。)^ 2;
pdist2
会给出距离。第一个sum
命令将沿着行进行求和,然后第二个将沿着列进行求和以给出矩阵中所有值的总和(这是您对两个用于
循环)。现在,。^ 2
背后的原因是:
在你的链接中原来的pdist2
已经提供,你可以从下面的代码片段中看到:
if(nargin< 3 || isempty(metric));度量= 0;结束;
switch metric
case {0,'sqeuclidean'}
平方欧几里得是默认距离,而在MATLAB中,欧几里得距离是默认的。所以,我已经把这个词汇平分了。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%I have written a function that is supposed to do the following:
- Take as an input two sets
- Take the distance between the two sets using
pdist2
which code is shown here. - It will take the distance between the two sets at the beginning. Then, for the second set, at each iteration, it will set the (i,j) location to
0
and calculate the distance with this change. An, when it goes to the next iteration, it should change the next location value to '0' while at the same time return the preceding value which was set to '0' to its original value. - Note that the result from
pdist2
originally return as a matrix, but for the comparison, I sum up the matrix values to use them for comparison.
Based on that, I have written the following function (note that you can use the
pdist2.m
function from the link here):function m = pixel_minimize_distance(x,y) sum1=0; sum2=0; [r c] = size(y); d1 = pdist2(x,y); [r1 c1] = size(d1); for i=1:r1 for j=1:c1 sum1=sum1+d1(i,j); end end maximum = sum1; for i=1:r for j=1:c o = y(i,j) y(i,j) = 0; d2 = pdist2(x,y); [r2 c2] = size(d2); for i=1:r2 for j=1:c2 sum2=sum2+d2(i,j); end end if sum2 >= maximum if o ~= 0 maximum = sum2; m = o; end end if sum2 <= maximum maximum = maximum; end y(i,j)=o; end end end
Now, this is what I have run as a test:
>> A=[1 2 3; 6 5 4]; >> B=[4 5 3; 7 8 1]; >> pixel_minimize_distance(A,B) o = 4 o = 4 o = 1 o = 7 o = 7 o = 0 ans = 7
See the the answer here is
7
(scroll down if you cannot see it), while the expected value when I calculate this manually should be3
, as since when we set it to0
the sum of the distance will be142
.Any idea what could be wrong in the code? I think it would be in the location in the code of setting
o = y(i,j)
whereo
denotes original value, but really couldn't figure a way of solving that.Thanks.
解决方案I think you have many redundant commands in your code. I just removed them, nothing else. I am getting value of
m
as3
. I used MATLAB'spdist2
function with squared euclidean distance (since that is the default in the function you provided). I did not get 142 as the distance.Here is the code:
function m = pixel_minimize_distance(x,y) [r c] = size(y); maximum = (sum(sum(pdist2(x,y)))).^2; %explained below for i=1:r for j=1:c o = y(i,j); y(i,j) = 0 sum2 = (sum(sum(pdist2(x,y)))).^2; if sum2 >= maximum if o ~= 0 maximum = sum2; m = o; end end y(i,j)=o; end end end
and output is:
y =
0 5 3 7 8 1
y =
4 0 3 7 8 1
y =
4 5 0 7 8 1
y =
4 5 3 0 8 1
y =
4 5 3 7 0 1
y =
4 5 3 7 8 0
m =
3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Explanation: You have written the following snippet of code:
d2 = pdist2(x,y); [r2 c2] = size(d2); for i=1:r2 for j=1:c2 sum2=sum2+d2(i,j); end end
what this simply does is calculates the distance between two sets using
pdist2
and sums up the entire distance matrix to come up with one value stored insum2
in your case. Lets look at the my code:sum2 = (sum(sum(pdist2(x,y)))).^2;
pdist2
will give the distance. Firstsum
command will sum along the rows and then the second one will sum along the columns to give you a total of all values in the matrix (This is what you did with twofor
loops). Now, the reason behind.^2
is: In the originalpdist2
function in the link which you have provided, you can see from the following snippet of code:if( nargin<3 || isempty(metric) ); metric=0; end; switch metric case {0,'sqeuclidean'}
that squared Euclidean is the default distance, whereas in MATLAB, Euclidean distance is default. Therefore, I have squared the term. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
这篇关于Matlab - 没有得到预期的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!