フィルターのクリア

How to create a long matrix rapidly

3 ビュー (過去 30 日間)
Sergio
Sergio 2024 年 1 月 17 日
編集済み: Dyuman Joshi 2024 年 1 月 17 日
I would like to create the given matrix
0 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1 1 1
1 1 1 1 0 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 0 1 1
1 1 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 1 1 0
I thought of using the command "zeros" for the diagonal, and then use "ones", but "ones" does not exist.
For instance:
m=[0 ones(1,9);0 ones(0,9)....]
But that won't work.
How can I make such a matrix with such "condensed" commands instead of writing it out completely?
Thanks

採用された回答

Mann Baidi
Mann Baidi 2024 年 1 月 17 日
編集済み: Mann Baidi 2024 年 1 月 17 日
Hi,
Assuming you would like to create a diagonal matrix with the diagonal as '0' and rest elements as '1'.
You can try the following code:
x=~diag(ones(10,1))
x = 10×10 logical array
0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0
For more information on "diag" function, you can refer to the link below:
Hope this will help in resolving the query!
  1 件のコメント
Sergio
Sergio 2024 年 1 月 17 日
Thanks! This is what I looked for indeed.

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

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2024 年 1 月 17 日
編集済み: Dyuman Joshi 2024 年 1 月 17 日
ones does exist, but using eye would be easier and faster here -
n = 10;
x = 1 - eye(n)
x = 10×10
0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0
  1 件のコメント
Sergio
Sergio 2024 年 1 月 17 日
Thanks Dyuman!

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

カテゴリ

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

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by