フィルターのクリア

How to operate on specific diagonal elements of a matrix?

4 ビュー (過去 30 日間)
Ahmad Gad
Ahmad Gad 2017 年 11 月 27 日
コメント済み: Ahmad Gad 2017 年 11 月 27 日
Hello everyone.
I am having a matrix k with size (20 x 20), for which, I need to replace some of the diagonal elements with ones. I constructed a vector v including the indices of the elements to be replaced, and applied the following code, but I is not working as I wanted.
k = rand(20);
u = [1; 3; 6; 11; 16; 20];
k(u(:,1),u(:,1)) = 1;
He is replacing the diagonal members and other members as if he is including all combinations of the rows and columns. How can I edit this to get only the diagonal members corrected?

採用された回答

Stephen23
Stephen23 2017 年 11 月 27 日
編集済み: Stephen23 2017 年 11 月 27 日
You need to use sub2ind:
>> idx = sub2ind(size(k),u,u);
>> k(idx) = 1
  1 件のコメント
Ahmad Gad
Ahmad Gad 2017 年 11 月 27 日
Thank you so much, it worked fine.

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

その他の回答 (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