Creating a new matrix from unique combinations in two other matrices
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Stephen23
on 23 Oct 2023
[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 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!