Creating a new matrix from unique combinations in two other matrices
1 次查看(过去 30 天)
显示 更早的评论
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!!!
0 个评论
采纳的回答
Stephen23
2023-10-23
[A,B] = meshgrid(repelem(1:3,3),1:3)
M = [reshape(A.',[],1),reshape(B.',[],1)];
[~,~,X] = unique(M,'rows','stable');
C = reshape(X,flip(size(A))).'
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!