How to create a long matrix rapidly
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Mann Baidi
on 17 Jan 2024
Edited: Mann Baidi
on 17 Jan 2024
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))
For more information on "diag" function, you can refer to the link below:
Hope this will help in resolving the query!
More Answers (1)
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!