Matching rows in different matrices

1 view (last 30 days)
Muneer
Muneer on 9 Dec 2013
Edited: Azzi Abdelmalek on 9 Dec 2013
I have a time array = [0 0.06 .12 .18 .24 ... 7684.3] which has a total of 128072 (128072x1) elements.
I have a separate matrix called timedata with time in the first column (has some repeated elements) and a data value in the second column. It looks like this:
0 0
0 0
.06 0
.12 0
.18 0
.24 0
... ...
7684.3 0
Its dimensions are 128425x2 because it has some repeated elements (as you can see 0 is repeated twice because I combined two matrices together and then sorted them in ascending order). I want to use my time vector as a control and pick the values from timedata that match. So the first val in "time" is 0, take the whole row of "timedata" where the first column is 0. The second val in "time" is .06 so I want to take the row that matches in "timedata". This would give me the third row and skip the second. At the end of this process, I would have a matrix with the same number of rows as "time". Any ideas on how to do this? I tried using a for loop like this
for i = 1:length(time)
new(i) = timedata(time(i), i);
end
But I don't think I'm indexing correctly. Any help would be greatly appreciated. Thanks in advance.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 9 Dec 2013
Edited: Azzi Abdelmalek on 9 Dec 2013
array = [0 0.06 .12 .18 .24 ];
v=[0 0
0 0
0.06 0
0.12 0
0.18 0
0.24 0]
w=unique(v,'rows','stable');
idx=ismember(w(:,1),array);
out=w(idx,:)

Community Treasure Hunt

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

Start Hunting!