フィルターのクリア

How to replace diagonal of square matrices within a cell array?

1 回表示 (過去 30 日間)
Adnan Habib
Adnan Habib 2021 年 9 月 11 日
コメント済み: Star Strider 2021 年 9 月 13 日
I have a 940X1 cell array where each element is a 298X298 square matrix. I want to replace the main diagonals of each square matrix with zero. Is there a way to do it?

採用された回答

Star Strider
Star Strider 2021 年 9 月 11 日
The easiest way is to subtract the diagonal of each matrix from the matrix —
C = {rand(4); rand(4); rand(4)}
C = 3×1 cell array
{4×4 double} {4×4 double} {4×4 double}
Cr = cellfun(@(x) x-diag(x), C, 'Unif',0)
Cr = 3×1 cell array
{4×4 double} {4×4 double} {4×4 double}
Cr{1}
ans = 4×4
0 0.5651 0.1597 0.4081 0.0049 0 -0.5032 0.3582 0.4049 0.8147 0 0.2985 -0.4793 -0.4366 -0.5270 0
Cr{3}
ans = 4×4
0 0.2540 -0.2275 -0.0101 0.1521 0 -0.7862 -0.3234 0.5575 -0.0462 0 -0.1965 -0.4953 -0.3632 -0.5305 0
Make appropriate changes to get different results.
.
  8 件のコメント
Adnan Habib
Adnan Habib 2021 年 9 月 13 日
Cheers! It is all fixed now. Really appreciate all your help.
Star Strider
Star Strider 2021 年 9 月 13 日
As always, my pleasure!
.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by