Matching rows in different matrices

1 次查看(过去 30 天)
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.

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-12-9
编辑:Azzi Abdelmalek 2013-12-9
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,:)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by