Using array values as indices for inserting values into a larger destination array

Hello all,
I have 3 arrays, one containing row indices, one containing column indices, and one containing the corresponding value at the matching row and column location.
I am trying to use the indices arrays to place the corresponding values into a larger array at the locations specified by the row and column indices arrays.
Here is a simplified example:
rowIndices = magic(8);
colIndices = magic(8);
values = 1:8;
values = repmat(values',1,8);
destinationArray = zeros(max(rowIndices(:)),max(colIndices(:)));
Basically, values(1,1) should be placed into destinationArray at rowIndices(1,1), colIndices(1,1) but for all (row,col,val) combinations at once.
I am using a quite large dataset and am trying to avoid loops whenever possible so I was hoping there is an efficient way to do this with a limited number of operations.
Thank you for your time and if you can help I'll acknowledge you in my dissertation!
-Chris H.

1 件のコメント

Stephen23
Stephen23 2019 年 3 月 27 日
Note that both magic calls will return the same output matrix, so your example will only allocate data to the diagonal of the destination matrix.

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

 採用された回答

Stephen23
Stephen23 2019 年 3 月 27 日
編集済み: Stephen23 2019 年 3 月 27 日
Use sub2ind:
N = 8;
R = randi(9,N); % more interesting than MAGIC.
C = randi(9,N); % more interesting than MAGIC.
V = reshape(1:N*N,N,N); % non-repeating example values.
S = [max(R(:)),max(C(:))];
Z = zeros(S);
Z(sub2ind(S,R,C)) = V

1 件のコメント

Christopher
Christopher 2019 年 3 月 27 日
This is exactly what I was looking for, thank you so much for the solution and the helpful testing comments!!!!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDiscrete Multiresolution Analysis についてさらに検索

製品

リリース

R2018a

質問済み:

2019 年 3 月 27 日

コメント済み:

2019 年 3 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by