フィルターのクリア

random matrix diagonal problem

2 ビュー (過去 30 日間)
yuval ohayon
yuval ohayon 2018 年 7 月 15 日
編集済み: Jan 2018 年 7 月 15 日
hi,how can I generates random matrix (using rand command) that's only its diagonal is zeros im trying to something like this but it seems the syntax is wrong\
v=[0 0 0 0 0 ]
b=rand[5,5]
c=b(diag(v) ????
I want that "c" will be 5*5 matrix the numbers are random except the diagonal that should be zeroes thank :}

回答 (2 件)

Matt J
Matt J 2018 年 7 月 15 日
編集済み: Matt J 2018 年 7 月 15 日
Here's another possibility. Its advantage is that it doesn't involve the creation of an additional matrix, although that would really only be important for larger matrix sizes.
c=rand(5);
c(1:6:end)=0;

Jan
Jan 2018 年 7 月 15 日
編集済み: Jan 2018 年 7 月 15 日
Or:
c = rand(5,5) .* (1 - eye(5))
Matt J's method requires less arithmetic operations and is more powerful, because it allows to set the elements to any value.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by