Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Matlab中增加矩阵的特定坐标_Matlab_Vector_Matrix_Operation - Fatal编程技术网

在Matlab中增加矩阵的特定坐标

在Matlab中增加矩阵的特定坐标,matlab,vector,matrix,operation,Matlab,Vector,Matrix,Operation,我试图找到矩阵中某些坐标的和 我有一个nxm矩阵。我有一个向量,它包含2xM值。向量中的每一对值都是矩阵中的一个坐标。因此,它们是M坐标数。我想在不使用for循环的情况下找到所有坐标的总和 我能用矩阵运算得到这个吗 谢谢如果您想找到2xM数组coords的质心,那么您只需编写 centroid = mean(coords,2) 如果要查找加权质心,其中每个坐标对由MxN数组A中的相应条目加权,可以使用sub2ind如下所示: idx = sub2ind(size(A),coords(1,:)'

我试图找到矩阵中某些坐标的和

我有一个
nxm
矩阵。我有一个向量,它包含
2xM
值。向量中的每一对值都是矩阵中的一个坐标。因此,它们是
M
坐标数。我想在不使用for循环的情况下找到所有坐标的总和

我能用矩阵运算得到这个吗


谢谢

如果您想找到
2xM
数组
coords
的质心,那么您只需编写

centroid = mean(coords,2)
如果要查找加权质心,其中每个坐标对由
MxN
数组
A
中的相应条目加权,可以使用
sub2ind
如下所示:

idx = sub2ind(size(A),coords(1,:)',coords(2,:)');

weights = A(idx);

weightedCentroid = sum( bsxfun( @times, coords', weights), 1 ) / sum(weights);
如果所需的只是坐标指向的所有条目的总和,则可以执行上述操作并简单地求和权重:

idx = sub2ind(size(A),coords(1,:)',coords(2,:)');

weights = A(idx);

sumOfValues = sum(weights);

如果您想找到
2xM
数组
coords
的质心,那么您只需编写

centroid = mean(coords,2)
如果要查找加权质心,其中每个坐标对由
MxN
数组
A
中的相应条目加权,可以使用
sub2ind
如下所示:

idx = sub2ind(size(A),coords(1,:)',coords(2,:)');

weights = A(idx);

weightedCentroid = sum( bsxfun( @times, coords', weights), 1 ) / sum(weights);
如果所需的只是坐标指向的所有条目的总和,则可以执行上述操作并简单地求和权重:

idx = sub2ind(size(A),coords(1,:)',coords(2,:)');

weights = A(idx);

sumOfValues = sum(weights);

据我所知,向量包含矩阵元素的(行、列)坐标。您可以将它们转换为矩阵元素数索引。这个例子展示了如何做到这一点。我假设你的坐标向量是这样的: [n-坐标1 m-坐标1 n-坐标2 m-坐标2…]

n = 5; % number of rows
m = 5; % number of columns
matrix = round(10*rand(n,m)); % An n by m example matrix
% A vector with 2*m elements. Element 1 is the n coordinate, 
% Element 2 the m coordinate, and so on. Indexes into the matrix:
vector = ceil(rand(1,2*m)*5); 
% turn the (n,m) coordinates into the element number index:
matrixIndices = vector(1:2:end) + (vector(2:2:end)-1)*n); 
sumOfMatrixElements = sum(matrix(matrixIndices)); % sums the values of the indexed matrix elements

据我所知,向量包含矩阵元素的(行、列)坐标。您可以将它们转换为矩阵元素数索引。这个例子展示了如何做到这一点。我假设你的坐标向量是这样的: [n-坐标1 m-坐标1 n-坐标2 m-坐标2…]

n = 5; % number of rows
m = 5; % number of columns
matrix = round(10*rand(n,m)); % An n by m example matrix
% A vector with 2*m elements. Element 1 is the n coordinate, 
% Element 2 the m coordinate, and so on. Indexes into the matrix:
vector = ceil(rand(1,2*m)*5); 
% turn the (n,m) coordinates into the element number index:
matrixIndices = vector(1:2:end) + (vector(2:2:end)-1)*n); 
sumOfMatrixElements = sum(matrix(matrixIndices)); % sums the values of the indexed matrix elements

坐标之和?还是这些坐标的值之和?坐标之和?或者这些坐标值的和?是的,sub2ind函数。它比我的向量(1:2:end)+(向量(2:2:end)-1)*n)更漂亮。但是我知道索引是在向量中,而不是在矩阵中。@solimanelefant:我假设
2xM
意味着
2xM
,所以索引应该在数组的每一列中。不过,我同意,问题并不是非常清楚。哦,是的,sub2ind函数。它比我的向量(1:2:end)+(向量(2:2:end)-1)*n)更漂亮。但是我知道索引是在向量中,而不是在矩阵中。@solimanelefant:我假设
2xM
意味着
2xM
,所以索引应该在数组的每一列中。不过,我同意,这个问题还不是非常清楚。