Finding the equal elements in 2 matrices
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello,
I've been looking online for a code that would help me with this, but I haven't found anything useful yet. I hope you can help me with this ! Let's say we have 2 matrices: Important point is that it IS guaranteed that there is 1 and only 1 element in each row of a & b that are the same. for example in row 1, 2 is the same, and in row 2, 3 is the same in this case. The column index of the elements are not necessarily the same. for example in row 1, the element "2" is in the 2nd column, but in b "2" is the first column. I need a code that would extract all these values and return a column vector with of all the common values, and this column vector MUST be in the same order as a and b. so basically in this case I want this code to give me : FYI, I have looked into ismember/ intersect functions already!
a =
1 2
3 4
b =
2 5
3 0
C =
2
3
I also have this code, which makes sense to me mathematically, but I idk why it's not giving me the correct answer :
tf = abs(a-b) < 1e-1 | abs(fliplr(a)-b) < 1e-1;
C = a(tf);
Thank you for your time!
채택된 답변
You need to take into account that MATLAB operates column-wise by transposing your matrices:
>> a = [90,50;20,11;0,87;11,110]
>> b = [41,90;11,31;87,1;11,-2]
>> at = a.';
>> bt = b.';
>> xt = at==bt | at==bt([2,1],:)
>> at(xt)
ans =
90
11
87
11
This is your example data form your earlier comments. Please do not post the same question in multiple locations, it makes it vary hard for us to keep track of what information you have been given. Of course the examples you give here also work:
>> a = [1,2;3,4];
>> b = [2,5;3,0];
>> at = a.';
>> bt = b.';
>> xt = at==bt | at==bt([2,1],:);
>> at(xt)
ans =
2
3
Or using your method with a tolerance:
>> tf = abs(at-bt) < 1e-1 | abs(at-bt([2,1],:)) < 1e-1;
>> at(tf)
ans =
2
3
Note in all cases I used only the transposed matrices, because MATLAB operates columns-wise.
댓글 수: 13
Thank you! I'm sorry I just thought I'll get more views making a new thread. Thank you !
Stephen,
Sorry to bother again, but I have another question. so the vector that this is giving me has less columns than my original matrices. Do you know why this happens and how can I fix it? I really appreciate you taking the time.
LA=[A B];
LB=[C D];
L1 = LA.';
L2 = LB.';
LL = L1==L2 | L1==L2([2,1],:);
G=L1(LL);
All L matrices have 14001 rows. but G has 11532 rows.
I guess there are some rows without matching values. Try this:
find(~any(LL,2))
to get the rows without any matching elements.
It's weird. it's giving me this hahaha
Empty matrix: 0-by-1
try this:
[R,C] = find(all(LL==false,2))
If they are also empty then can you please upload those variables in a file, and I will take a look.
They were also empty. M files are attached. Thank you so much!
You did not provide the function AAE590_HW5, which is used in the ode45 call.
It might be easier if you simply put LA and LB into a mat file and upload it here. (add ".txt" to its filename).
I just uploaded LA and LB
The two files you uploaded have exactly the same data in them:
>> LA = load('LA.m','txt');
>> LB = load('LB.m','txt');
>> isequal(LA,LB)
ans =
1
Each has 5307 rows, and the output has 10614 elements. This means the code is performing as it should.
I'm so sorry. I copied pasted the same one. here you go this is LB.
There are 870 rows without any matching values. This will locate them:
>> find(~any(LL,1))
ans =
Columns 1 through 8
1725 1726 1727 1728 1729 1730 1731 1732
Columns 9 through 16
1733 1734 1735 1736 1737 1738 1739 1740
... lots more here
Columns 857 through 864
4454 4455 4456 4457 4458 4459 4460 4461
Columns 865 through 870
4462 4463 4464 4465 4466 4467
>> numel(ans)
ans =
870
oh ok , thank you!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
