Arrays 向量值之差的最大值

Arrays 向量值之差的最大值,arrays,matlab,sorting,Arrays,Matlab,Sorting,我有一个向量: A = 0.5743 -0.3167 0.0591 -0.2576 0.0000 0.2576 -0.0591 0.3167 -0.5743 我想将数组A的每个值之间的差值存储在数组或矩阵中,并找到最大差值,然后输出产生最大值的索引(例如,v(1)-v(3)产生最大值)。有人有什么建议吗?这就是你想要的吗 d = abs(bsxfun(@minus, A, A.')); %'// compute all diffe

我有一个向量:

A =
    0.5743
   -0.3167
    0.0591
   -0.2576
    0.0000
    0.2576
   -0.0591
    0.3167
   -0.5743
我想将数组A的每个值之间的差值存储在数组或矩阵中,并找到最大差值,然后输出产生最大值的索引(例如,v(1)-v(3)产生最大值)。有人有什么建议吗?

这就是你想要的吗

d = abs(bsxfun(@minus, A, A.')); %'// compute all differences
[~, ind] = max(d(:)); %// find linear index of maximum difference
[row, col] = ind2sub([numel(A) numel(A)], ind); %// convert to row and column
所寻求的索引由变量
给出。差分矩阵是
d

或者,第一行可以替换为

d = squareform(pdist(A));
这是你想要的吗

d = abs(bsxfun(@minus, A, A.')); %'// compute all differences
[~, ind] = max(d(:)); %// find linear index of maximum difference
[row, col] = ind2sub([numel(A) numel(A)], ind); %// convert to row and column
所寻求的索引由变量
给出。差分矩阵是
d

或者,第一行可以替换为

d = squareform(pdist(A));
这是你想要的吗

d = abs(bsxfun(@minus, A, A.')); %'// compute all differences
[~, ind] = max(d(:)); %// find linear index of maximum difference
[row, col] = ind2sub([numel(A) numel(A)], ind); %// convert to row and column
所寻求的索引由变量
给出。差分矩阵是
d

或者,第一行可以替换为

d = squareform(pdist(A));
这是你想要的吗

d = abs(bsxfun(@minus, A, A.')); %'// compute all differences
[~, ind] = max(d(:)); %// find linear index of maximum difference
[row, col] = ind2sub([numel(A) numel(A)], ind); %// convert to row and column
所寻求的索引由变量
给出。差分矩阵是
d

或者,第一行可以替换为

d = squareform(pdist(A));

我不认为你需要计算所有的差异。你只需要找到最大值和最小值,然后减去

[max_val, max_ind] = max(A(:));
[min_val, min_ind] = min(A(:));
disp(['Max difference is ', num2str(max_val - min_val), ' which is the difference between indicies ', num2str(max_ind), ' and ', num2str(min_ind)]);

我不认为你需要计算所有的差异。你只需要找到最大值和最小值,然后减去

[max_val, max_ind] = max(A(:));
[min_val, min_ind] = min(A(:));
disp(['Max difference is ', num2str(max_val - min_val), ' which is the difference between indicies ', num2str(max_ind), ' and ', num2str(min_ind)]);

我不认为你需要计算所有的差异。你只需要找到最大值和最小值,然后减去

[max_val, max_ind] = max(A(:));
[min_val, min_ind] = min(A(:));
disp(['Max difference is ', num2str(max_val - min_val), ' which is the difference between indicies ', num2str(max_ind), ' and ', num2str(min_ind)]);

我不认为你需要计算所有的差异。你只需要找到最大值和最小值,然后减去

[max_val, max_ind] = max(A(:));
[min_val, min_ind] = min(A(:));
disp(['Max difference is ', num2str(max_val - min_val), ' which is the difference between indicies ', num2str(max_ind), ' and ', num2str(min_ind)]);


我也会这么做的。。。尤其是
pdist
-+1。@rayryeng但是
bsxfun
也很酷。。。而且
pdist
需要统计工具箱:-)对不起,我是MATLAB新手。我运行了代码并收到错误:索引无法产生多个结果。Define
A=[0.5743-0.3167 0.0591-0.2576 0.0000 0.2576-0.0591 0.3167-0.5743]并粘贴我的代码尝试在开始处清除所有代码。另外,尝试将第二行替换为
[nada,ind]=max(d(:)。你有什么Matlab版本?这也是我应该做的。。。尤其是
pdist
-+1。@rayryeng但是
bsxfun
也很酷。。。而且
pdist
需要统计工具箱:-)对不起,我是MATLAB新手。我运行了代码并收到错误:索引无法产生多个结果。Define
A=[0.5743-0.3167 0.0591-0.2576 0.0000 0.2576-0.0591 0.3167-0.5743]并粘贴我的代码尝试在开始处清除所有代码。另外,尝试将第二行替换为
[nada,ind]=max(d(:)。你有什么Matlab版本?这也是我应该做的。。。尤其是
pdist
-+1。@rayryeng但是
bsxfun
也很酷。。。而且
pdist
需要统计工具箱:-)对不起,我是MATLAB新手。我运行了代码并收到错误:索引无法产生多个结果。Define
A=[0.5743-0.3167 0.0591-0.2576 0.0000 0.2576-0.0591 0.3167-0.5743]并粘贴我的代码尝试在开始处清除所有代码。另外,尝试将第二行替换为
[nada,ind]=max(d(:)。你有什么Matlab版本?这也是我应该做的。。。尤其是
pdist
-+1。@rayryeng但是
bsxfun
也很酷。。。而且
pdist
需要统计工具箱:-)对不起,我是MATLAB新手。我运行了代码并收到错误:索引无法产生多个结果。Define
A=[0.5743-0.3167 0.0591-0.2576 0.0000 0.2576-0.0591 0.3167-0.5743]并粘贴我的代码尝试在开始处清除所有代码。另外,尝试将第二行替换为
[nada,ind]=max(d(:)。你有什么Matlab版本?可以扩展它来找到最小值吗?还有,差平方的最小值呢?((A(1)-A(2))^2等)。我认为我们需要找到最大的两个或最小的两个,然后减去它们,然后取它的平方根。还有其他更简单的方法吗?是否可以扩展此方法以找到最小值?还有,差平方的最小值呢?((A(1)-A(2))^2等)。我认为我们需要找到最大的两个或最小的两个,然后减去它们,然后取它的平方根。还有其他更简单的方法吗?是否可以扩展此方法以找到最小值?还有,差平方的最小值呢?((A(1)-A(2))^2等)。我认为我们需要找到最大的两个或最小的两个,然后减去它们,然后取它的平方根。还有其他更简单的方法吗?是否可以扩展此方法以找到最小值?还有,差平方的最小值呢?((A(1)-A(2))^2等)。我认为我们需要找到最大的两个或最小的两个,然后减去它们,然后取它的平方根。还有其他更简单的方法吗?