How to subtract each column of a matrix from a column vector?

29 ビュー (過去 30 日間)
M
M 2022 年 3 月 31 日
回答済み: Image Analyst 2022 年 4 月 1 日
How to subtract each column of a matrix from a column vector?

回答 (2 件)

James Tursa
James Tursa 2022 年 3 月 31 日
編集済み: James Tursa 2022 年 3 月 31 日
Just do a usual subtraction and let MATLAB implicit expansion kick in:
M = your matrix
v = your column vector
result = v - M; % implicit expansion

Image Analyst
Image Analyst 2022 年 4 月 1 日
If you don't have a version that has implicit conversion yet, you can do this, where v is your vector and m is your matrix.
% Create sample data.
v = randi(99, 3, 1)
v = 3×1
28 23 78
m = randi(99, 3, 3)
m = 3×3
82 23 62 42 48 78 67 16 83
[rows, columns] = size(m);
diffMatrix = zeros(rows, columns);
% Subtract each column of m one at a time from v.
for col = 1 : columns
diffMatrix(:, col) = v - m(:, col);
end
% Show result in command window:
diffMatrix
diffMatrix = 3×3
-54 5 -34 -19 -25 -55 11 62 -5

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by