Multipying each element of a matrix with average of elements in other matrix

1 回表示 (過去 30 日間)
Hari
Hari 2021 年 7 月 14 日
コメント済み: Hari 2021 年 7 月 14 日
I have a 3x3 matrix(A) with all diagonal elements 0, and 3x1 matrix(B). I want to multiply each element of the 3x3 matrix using elements from the 3x1 matrix in the following way: element A(1,2) will be multipled by the average of elements at position 1 and 2 in B.
Similarly each element in A will be multiplied by average of corresponding elements in B.
Is there a way to do this.

採用された回答

Simon Chan
Simon Chan 2021 年 7 月 14 日
Try this:
[Ny,Nx]=size(A);
[X,Y]=meshgrid(1:Nx,1:Ny);
w = (B(X)+B(Y))/2;
C = A.*w

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 7 月 14 日
Did you try the obvious and simple for loop?
% Create sample data.
A = rand(3);
A(logical(eye(3))) = 0
B = rand(3, 1)
% Now we have our data and can begin...
for col = 1 : 3
for row = 1 : 3
factor = mean([B(row), B(col)]);
A(row, col) = factor * A(row, col);
end
end
A % Show in command window.
  1 件のコメント
Hari
Hari 2021 年 7 月 14 日
This works. But wouldn't this be slow if the matrix is very large?

サインインしてコメントする。

カテゴリ

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