Create matrix row element if row elements of two previous matrices are identical
Show older comments
Sorry for the title. I could not think of something better.
I have the following problem.
I have two four-column matrices build up like this:
Property | X | Y | Z
The two matrices have different sizes, since matrix 1 has a large amount of additional rows compared to matrix 2.
What I want to do is the following:
I need to create a third matrix that only features those rows (of the large matrix) that are identical in columns X, Y and Z to rows in matrix2(the property column is always different).
I tried an if-statement but it did not really work out due to my programming syntax. Has somebody a tip?
Thank you!
Example - I tried something like this: (in this case A is the larger matrix and I want its property column for X,Y,Z-positions that are identical to another matrix B.. I am terrible with the MatLab-syntax..:
if (A(:,2) == B(:,2) and (A(:,3) == B(:,3) and (A(:,4) == B(:,4))
newArray(:,1) = A(:,1);
end
I fear that in the way depicted in the code example it might cancel since A and B are of different size and it cannot really compare
Accepted Answer
More Answers (1)
Thorsten
on 26 Nov 2014
In this example A is the large matrix and B the small one
A = round(10*rand(10, 3)); A = [A; A(1:2, :)];
B = A(1:2:end, :);
A = [rand(size(A, 1), 1) A];
B = [rand(size(B, 1), 1) B];
Create the new A
Anew = A(ismember(A(:, 2:4), B(:, 2:4), 'rows'), :);
Categories
Find more on Resizing and Reshaping Matrices 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!