Creating a new matrix from unique combinations in two other matrices

1 回表示 (過去 30 日間)
Victoria Dutch
Victoria Dutch 2023 年 10 月 23 日
回答済み: Stephen23 2023 年 10 月 23 日
I have two matrices (A & B), each with the same dimensions. Each matrix contains values to divide my data into different bins. How do I create a third matrix (C) of the unique combinations of values in A and B, so that I can then use matrix C to get the mean values of grid points from a different variable at all indices with the same value in C.
I.E., If
A =
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
B =
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3
then the 3rd matrix (C) would be:
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
7 7 7 8 8 8 9 9 9
How do I create C? (The actual dataset is much larger than that this)
Thanks!!!

採用された回答

Stephen23
Stephen23 2023 年 10 月 23 日
[A,B] = meshgrid(repelem(1:3,3),1:3)
A = 3×9
1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3
B = 3×9
1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3
M = [reshape(A.',[],1),reshape(B.',[],1)];
[~,~,X] = unique(M,'rows','stable');
C = reshape(X,flip(size(A))).'
C = 3×9
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by