How to add sparse matrix to an n*n matrix

1 回表示 (過去 30 日間)
SALMAN KHAN
SALMAN KHAN 2022 年 3 月 5 日
コメント済み: SALMAN KHAN 2022 年 3 月 5 日
Let say;
A = ones(4,4);
i = [2 4]; j = [1,3];
B = sparse(i,j,[9 9]);
A + B;
How can I add A+B, so that the component A(2,1) = 1+9 and A(4,3) = 1+9. Rest of the components of matrix A should be 1. I am trying the above snippet but it outputs error: Error using +
Please help me if its possible to add accordingly, Thanks.

採用された回答

KSSV
KSSV 2022 年 3 月 5 日
To add A and B, they should be of same dimension. In your case A is 4x4 matrix whereas B is not. So to add them, you have to make B to the same size of A.
A = ones(4,4);
i = [2 4]; j = [1,3];
B = sparse(i,j,[9 9],4,4) ;
iwant = A + B
iwant = 4×4
1 1 1 1 10 1 1 1 1 1 1 1 1 1 10 1
  1 件のコメント
SALMAN KHAN
SALMAN KHAN 2022 年 3 月 5 日
Thanks. It helps me alot.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by