Asign data with index or something...

2 ビュー (過去 30 日間)
Gimpy
Gimpy 2014 年 8 月 21 日
回答済み: Sean de Wolski 2014 年 8 月 21 日
Hi, I have the following inputs:
row=[3,7,10]
col=[1,2,3]
data=rand(1,3)
data =
0.91 0.63 0.10
matrix=zeros(3,3)
And I want to replace the following row in matrix: row 3, coll 7 by 0.81 instead of 0
row 7, coll 2 by 0.63 instead of 0
row 10, coll 3 by 0.1 instead of 0
Any idea?
PS: wihtout loop and the real matrix a much more biger and it need to be dynamic the answer.
Thank you!

採用された回答

Sean de Wolski
Sean de Wolski 2014 年 8 月 21 日
You've defined matrix to be a 3x3 but want to reference the 7th and 10th row. How big should it be?
If you just want it to be "big enough"
M = zeros(max(row),max(col));
M(sub2ind(size(M),row,col)) = data

その他の回答 (1 件)

Matt J
Matt J 2014 年 8 月 21 日
matrix = sparse(row,col,data)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by