Matching rows in different matrices

조회 수: 1 (최근 30일)
Muneer
Muneer 2013년 12월 9일
편집: Azzi Abdelmalek 2013년 12월 9일
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