I have two matrix data1 and data2. I want to select all rows in data1 that close or equals to data2. I know that loop each element can work but it will take a long time. Is there any easy method I can use?
data1 is attached and data 2 is the following function.
x1 = (0:1:414194)';
data2 = 6*x1;
Thank a lot,
MCC

댓글 수: 1

SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 15일
what is the matrices size of data1 and data2!

댓글을 달려면 로그인하십시오.

 채택된 답변

SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 15일

0 개 추천

Here you can extract those element val that are in both data located at index1 and index2
[val,index1,index2] = intersect(data1,data2)

댓글 수: 4

MCC
MCC 2021년 6월 15일
Hi, I tried to used this method. But it will extract only part of the data. Any ideas?
Thanks.
SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 15일
it will extrac the common values, what else do you want to get!
Another approach, to find values in a that are close or equal to data in b. Eror 0.001
a=1:10;
b=[3.001,4.99,5,6,10];
[x,y] = meshgrid(a,b);
A = [x(:),y(:)];
index = find(abs(A(:,1)-A(:,2))<1e-3);
x(index) % values in a that are close or equal to data in b
MCC
MCC 2021년 6월 15일
Thanks. This works perfet for my problem.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 15일

0 개 추천

Easy and fast efficient solution is Logical Indexing, e.g.:
IND = Data1==Data2
D1 = Data1(IND);
D2 = Data2(IND);
You can also employ round() fcn to reduce the small differences in values of the two data sets.
Or even better to compute the min. differences and get those indices, e.g.:
[minVal,ClosestIND] = min(abs(Data1-Data2))
D1 = Data1(ClosestIND);
D2 = Data2(ClosestIND);

댓글 수: 3

MCC
MCC 2021년 6월 15일
Hi Sulaymon,
The rows of data1 and data2 are not identical. So it makes this diffcult to handle.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 15일
In that case, probably a loop would be an opt.
MCC
MCC 2021년 6월 15일
편집: MCC 2021년 6월 15일
Yes. Do you have any ideas of how to write this code? The data1 is 47987 by 1 and the data2 is 414195 by 1.
Thanks a lot,
MCC

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

MCC
2021년 6월 15일

댓글:

MCC
2021년 6월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by