Populate a matrix using the elements of some columns of another matrix as index and other as values

3 ビュー (過去 30 日間)
I have a matrix with 4 columns. I want to use the elements of the first 3 columns as index and the element of the 4th column as value to create a new matrix. I also need the values with the same index to be summed. I can do that row by row. However, I was wondering if there is a more elegant way, with no loop.
myData = [1 2 1 1
1 2 2 2
3 1 3 1
1 2 3 1
2 3 2 3
1 2 2 1];
newMat = zeros(3,3,3);
for row = myData'
newMat(row(2),row(3),row(1)) = newMat(row(2),row(3),row(1)) + row(4);
end
newMat

採用された回答

Stephen23
Stephen23 2023 年 7 月 6 日
編集済み: Stephen23 2023 年 7 月 6 日
" may have multiple values with the same indices. In my case, I need to add the values with the same index"
That is exactly what ACCUMARRAY is for:
A = [1,2,1,1; 1,2,2,2; 3,1,3,1; 1,2,3,1; 2,3,2,3; 1,2,2,1]
A = 6×4
1 2 1 1 1 2 2 2 3 1 3 1 1 2 3 1 2 3 2 3 1 2 2 1
B = accumarray(A(:,[2,3,1]),A(:,4),[],@sum)
B =
B(:,:,1) = 0 0 0 1 3 1 0 0 0 B(:,:,2) = 0 0 0 0 0 0 0 3 0 B(:,:,3) = 0 0 1 0 0 0 0 0 0
That is an odd order to store the indices in. Why not store them in order of the dimensions?
  2 件のコメント
Stephen23
Stephen23 2023 年 7 月 6 日
For comparison, the loop provided by the OP:
myData = [1 2 1 1
1 2 2 2
3 1 3 1
1 2 3 1
2 3 2 3
1 2 2 1];
newMat = zeros(3,3,3);
for row = myData'
newMat(row(2),row(3),row(1)) = newMat(row(2),row(3),row(1)) + row(4);
end
newMat
newMat =
newMat(:,:,1) = 0 0 0 1 3 1 0 0 0 newMat(:,:,2) = 0 0 0 0 0 0 0 3 0 newMat(:,:,3) = 0 0 1 0 0 0 0 0 0
FredMat
FredMat 2023 年 7 月 6 日
Hi,
Thanks for the pointer. I guess I didn't search long enough.

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

その他の回答 (1 件)

Sahas Marwah
Sahas Marwah 2023 年 7 月 6 日
You can use the "sub2ind" function in MATLAB to convert the subscripts of the new matrix into linear indices, and then assign the values directly.
Here is my sample of the code:
myData = [1 2 1 1
1 2 2 2
3 1 3 1
1 2 3 1
2 3 2 3];
indices = myData(:, 1:3);
values = myData(:, 4);
newMat = zeros(3, 3, 3);
linearIndices = sub2ind(size(newMat), indices(:, 2), indices(:, 3), indices(:, 1));
newMat(linearIndices) = values;
newMat
Here is the documentation link to "sub2ind" for reference:
  1 件のコメント
FredMat
FredMat 2023 年 7 月 6 日
Hi,
Thanks for your answer. It works fine. However, it made me realize that I may have multiple values with the same indices. In my case, I need to add the values with the same index. I modified my code to reflect this constraint.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by