How to replace diagonal of square matrices within a cell array?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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?
採用された回答
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 件のコメント
Awesome! Thanks a lot!
As always, my plesaure!
Replacing the diagonals with different values is simply an extension of that approach —
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)+diag([1 2 3 4]), C, 'Unif',0)
Cr = 3×1 cell array
{4×4 double}
{4×4 double}
{4×4 double}
Cr{1}
ans = 4×4
1.0000 0.1342 -0.2432 0.2293
-0.6770 2.0000 -0.3277 0.1889
0.6493 0.5733 3.0000 0.7478
0.1705 0.4593 0.5354 4.0000
Cr{3}
ans = 4×4
1.0000 -0.7256 0.2266 -0.5666
0.0518 2.0000 -0.5614 -0.2324
-0.0021 0.7514 3.0000 0.3976
-0.5289 -0.0793 -0.1245 4.0000
.
Well it gets the job done. And this is easier.
Thank you!
.
Hi Star Strider.
Sorry to revive this post again. I just realized that by using your method although the diagonal is replaced by zeros, but the values around the diagonal element also changed. Why is that? The other values should remain the same.
It is because I have not used diag in a while, and assumed that it only changes the diagonal.
Change ‘Cr’ to:
Cr = cellfun(@(x) x-diag(diag(x)), C, 'Unif',0);
and it works the way I intend it to (and the way I thought it originally did).
My apologies for the inconvenience.
.
Cheers! It is all fixed now. Really appreciate all your help.
As always, my pleasure!
.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
