Sub values into multiple rows and columns simutaneously
古いコメントを表示
I have a 10x10 matrix. I want to sub a few values into the matrix simultaneously, with the other values being zero. It would be time-consuming to sub the values one by one. Is there any way to do this?
For example, A=[zeros(10,10)] A(1,1)=-4 A(2,2)=-4 A(3,3)=-4
Is there a way to sub -4 into these locations at once?
1 件のコメント
Jan
2018 年 1 月 19 日
Does "to sub" mean "subtract" or "substitute"? According to your code "substitute" is meant.
採用された回答
その他の回答 (2 件)
Walter Roberson
2018 年 1 月 19 日
0 投票
Use sub2ind convert the row column pairs into linear indexing. Then index the left side with the linear indexes.
Jos (10584)
2018 年 1 月 19 日
The words " a few" and " others remaining zero" might indicate that you also could use SPARSE matrices. For instance:
A = sparse([1 2 3],[1 2 3],-4,10,10)
full(A)
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!