You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将N×N对称矩阵行元素按N×1列向量逐行除法得到对称矩阵

Hey Leonardo, great question! Let's clarify what you're asking and walk through how to achieve this, whether you're working with Python (NumPy), MATLAB, or just mathematical notation.

问题明确

You need to take an N×N symmetric matrix ( A ), perform an operation where every element in the ( i )-th row ( A[i][j] ) (for all ( j ) from 1 to N) is divided by the ( i )-th element of an N×1 column vector ( v ), and end up with another N×N symmetric matrix ( B ).

First, let's break down the math behind this, then jump into practical implementations.

数学原理

Since ( A ) is symmetric, ( A[i][j] = A[j][i] ) for all ( i,j ).

Case 1: Strictly per-row division (only symmetric if ( v ) is constant)

If we define ( B[i][j] = \frac{A[i][j]}{v[i]} ), for ( B ) to be symmetric we need ( B[i][j] = B[j][i] ). Substituting the definition:

( \frac{A[i][j]}{v[i]} = \frac{A[j][i]}{v[j]} )

Since ( A[i][j] = A[j][i] ), this simplifies to ( \frac{1}{v[i]} = \frac{1}{v[j]} ), meaning all elements of ( v ) must be equal for ( B ) to stay symmetric.

Case 2: Element-wise division by ( v[i] \times v[j] ) (always symmetric)

If your goal is a symmetric matrix regardless of ( v ), you likely want this operation (common in stats when converting covariance matrices to correlation matrices):
( B[i][j] = \frac{A[i][j]}{v[i] \times v[j]} )

This guarantees symmetry because:

( B[i][j] = \frac{A[i][j]}{v[i]v[j]} = \frac{A[j][i]}{v[j]v[i]} = B[j][i] )

I suspect this is the operation you're actually aiming for, since it produces a symmetric matrix no matter what non-zero ( v ) you use.

代码实现

1. Strict per-row division (symmetric only if ( v ) is constant)

Python (NumPy)

import numpy as np

# Example symmetric matrix
A = np.array([[4, 2, 1],
              [2, 5, 3],
              [1, 3, 6]])
# Constant column vector (ensures symmetric result)
v = np.array([2, 2, 2]).reshape(-1, 1)

# Divide each row by the corresponding element in v
B = A / v  # NumPy automatically broadcasts v to a row vector

print("Result matrix B:")
print(B)
print("Is symmetric?", np.allclose(B, B.T))

MATLAB

% Example symmetric matrix
A = [4 2 1; 2 5 3; 1 3 6];
% Constant column vector
v = [2; 2; 2];

% Divide each row by the corresponding element in v
B = A ./ v;  % Element-wise division, MATLAB broadcasts v to a row vector

disp('Result matrix B:');
disp(B);
disp('Is symmetric?');
disp(all(all(B == B')));

2. Element-wise division by ( v[i] \times v[j] ) (always symmetric)

Python (NumPy)

import numpy as np

A = np.array([[4, 2, 1],
              [2, 5, 3],
              [1, 3, 6]])
v = np.array([2, 3, 4]).reshape(-1, 1)

# Compute outer product of v with itself, then element-wise division
B = A / (v @ v.T)

print("Result matrix B:")
print(B)
print("Is symmetric?", np.allclose(B, B.T))

MATLAB

A = [4 2 1; 2 5 3; 1 3 6];
v = [2; 3; 4];

% Compute outer product of v with itself, then element-wise division
B = A ./ (v * v');

disp('Result matrix B:');
disp(B);
disp('Is symmetric?');
disp(all(all(B == B')));
验证对称性

To confirm your result is symmetric:

  • In NumPy, use np.allclose(B, B.T) (accounts for floating-point precision errors)
  • In MATLAB, use all(all(B == B')) (works for exact integers or floats)

内容的提问来源于stack exchange,提问作者Leonardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:21:52