Reputation: 107
In MATLAB (R2015a), I have two large matrices that can be simplified as:
A = [ 24 10 830; 24 15 830; 150 17 945; 231 40 1130; 231 45 1130]
(note that in A
, column 3 is the time for the event cataloged in column 1) and
B = [24 13; 150 29; 231 43]
How can I match and copy the time-data from column 3 in matrix A
, to a matching and filtered event column in matrix B
?
For example, I want the value 24 from first column in B
to be matched with value 24 from first column in A
, and then copy the corresponding time-data in A
's third column (for 24 it's 830, for 150 it's 945 etc.) into B
. This should result into our new B
with the time-data from A
:
B = [24 13 830; 150 29 945; 231 43 1130]
I relatively new to MATLAB so any help is much appreciated!
Upvotes: 1
Views: 24
Reputation: 6434
First find the location of the elements in the first row of B
in the first row of A
using the ismember
function. And then use those locations to construct the new matrix.
[~,Locb] = ismember(B(:,1),A(:,1));
Bnew = [B A(Locb,3)]
Bnew =
24 13 830
150 29 945
231 43 1130
This is a fast way that comes to my mind. There might be some singularities that needed to be checked more thoroughly.
Upvotes: 2