vectorized non-linear matrix assignment

10 ビュー (過去 30 日間)
cmo
cmo 2015 年 5 月 18 日
コメント済み: John D'Errico 2015 年 5 月 18 日
Let A be an m x n matrix (both m,n > 1).
let "X" p-by-1 be a set of row indices of A, and "Y" p-by-1 be a set of column indices of "A".
Suppose I want to assign the values of vector "V" p-by-1 to coordinates "(X,Y)" of "A".
Is there a way to vectorize this assignment?
e.g.
A = zeros(10,10);
X = [1 4 5 9];
Y = [ 2 2 1 8];
V = [1.1 2.98 -3 14];
i.e.
I want to assign A(1,2) = 1.1 and A(4,2) = 2.98 and A(5,1) = -3 and A(9,8) = 14.
something like "A(X,Y) = V", but that doesnt work.

回答 (1 件)

Star Strider
Star Strider 2015 年 5 月 18 日
Using the sub2ind function is the easiest way:
LI = sub2ind(size(A), X', Y');
A(LI) = V
  1 件のコメント
John D'Errico
John D'Errico 2015 年 5 月 18 日
+1. While this is of course the correct answer, it has always bothered me that there is not a more direct solution. Use of sub2ind is just too un-obvious to the new user.

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

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by