在另一个矩阵中找到一个矩阵的共同值 [英] find common value of one matrix in another matrix

查看:186
本文介绍了在另一个矩阵中找到一个矩阵的共同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵 MyMatrix 2549x13double

I have one matrix MyMatrix 2549x13double

MyMatrix 中的几行示例:

-7.80   -4.41   -0.08   2.51    6.31    6.95    4.97    2.91    0.66    -0.92   0.31    1.24    -0.07
4.58    5.87    6.18    6.23    5.20    4.86    5.02    5.33    3.69    1.36    -0.54   0.28    -1.20
-6.22   -3.77   1.18    2.85    -3.55   0.52    3.24    -7.77   -8.43   -9.81   -6.05   -5.88   -7.77
-2.21   -3.21   -4.44   -3.58   -0.89   3.40    6.56    7.20    4.30    -0.77   -5.09   -3.18   0.43

我已经确定矩阵 MyMatrix 的每一行的最大值如下:

I have identified the maximum value for each row of matrix MyMatrix as following:

 [M Ind] = max(MyMatrix, [], 2);

我在 M 中获得的示例行:

6.95
6.23
3.24
7.20

现在,我想在MyMatrix中选择在 M 中找到的最大值之前和之后的2个值,因为我需要计算这5个值的平均值. 因此,在示例中,我想选择:

Now, I would like to select in MyMatrix the 2 values before and after the maximum value as found in M, as I will need to calculate the average of these 5 values. So, in the example, I would like to select:

2.51    6.31    6.95    4.97    2.91
5.87    6.18    6.23    5.20    4.86
-3.55   0.52    3.24    -7.77   -8.43
3.40    6.56    7.20    4.30    -0.77

,并使用这5个值的平均值在 MyMatrix 中创建一个新列.

and to create a new column in MyMatrix with the mean of these 5 values.

我将不胜感激. 非常感谢.

I would appreciate any help. Many thanks.

推荐答案

获取每行所需的列索引:

Get the column indices required per row:

colInd = bsxfun(@plus,Ind, -2:2)

现在,实际上,转置矩阵(MyMatrixT = MyMatrix.')会更容易,因为我们将使用线性索引,所以我们宁可使用

Now it will actually be easier to work with your matrix transposed (MyMatrixT = MyMatrix.') since we will be working with linear indexes so let's rather work with

rowIndT = colInd.';

现在,我们想将此Rind转换为线性索引.这只是将总行数(原始行数)加到列号上的情况

Now we want to convert this Rind to linear indexes. This is just a case of adding the total number of rows (number of columns in the original) to the column number

linIndT = bsxfun(@plus,rowIndT,0:size(MyMatrixT,1):size(MyMatrixT,1)*(size(MyMatrixT,2)-1))

最后我们提取值并转置回去

And finally we extract the values and transpose back

resultT = MyMatrixT(linIndT);
result = resultT.'

result =

    2.5100    6.3100    6.9500    4.9700    2.9100
    5.8700    6.1800    6.2300    5.2000    4.8600
   -3.5500    0.5200    3.2400   -7.7700   -8.4300
    3.4000    6.5600    7.2000    4.3000   -0.7700

新列只是结果的平均值:

The new column is just the mean of result:

mean(result,2)

并将其附加到您的矩阵中

and to append it to your matrix

MyMatrix = [MyMatrix, mean(result,2)]

现在仍然存在一个问题,如果最大值在边缘附近(即,如果最大值在第2列中,则未定义最大值之前的两个值)会发生什么.如何处理此问题将要求您首先定义在这种情况下所需的行为.但是,假设您想要NaN,那么我会这样做:

Now there is still one problem, what happens if a maximum is near the edge (i.e. if a maximum is in column 2, then two values before the maximum is not defined). How to deal with this will require you to first define the behavior you desire in such situations. But let's assume you want NaN, then I would do this:

colInd = bsxfun(@plus,Ind, -2:2);
rowIndT = colInd.';

%  Bound rowIndT to be between 1 and size(MyMatrixT,1)
rowIndT(rowIndT < 1) = 1;
rowIndT(rowIndT > size(MyMatrixT,1)) = size(MyMatrixT,1);

linIndT = bsxfun(@plus,rowIndT,0:size(MyMatrixT,1):size(MyMatrixT,1)*(size(MyMatrixT,2)-1)); % You can use sub2ind instead for this step
result = MyMatrixT(linIndT).';

% Now go back and put NaNs where they are needed
nanColInd = colInd < 1 | colInd > size(MyMatrix,2);
result(nanColInd) = NaN;
% Now use nanmean to ignore any NaNs when finding the mean
MyMatrix = [MyMatrix, nanmean(result,2)]

最后一件事,使用sub2ind查找线性索引可能会更直观.在这种情况下

One last thing, you might find it more intuitive to use sub2ind to find the linear indexes. In that case

linIndT = bsxfun(@plus,rowIndT,0:size(MyMatrixT,1):size(MyMatrixT,1)*(size(MyMatrixT,2)-1))

成为

linIndT = sub2ind(size(MyMatrixT), rowIndT, repmat(1:size(MyMatrixT,2),size(rowIndT,1),1))

这篇关于在另一个矩阵中找到一个矩阵的共同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆