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 件のコメント
Salar
2016 年 2 月 25 日
Thank you! I'm sorry I just thought I'll get more views making a new thread. Thank you !
Salar
2016 年 2 月 25 日
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.
Stephen23
2016 年 2 月 25 日
I guess there are some rows without matching values. Try this:
find(~any(LL,2))
to get the rows without any matching elements.
Salar
2016 年 2 月 25 日
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.
Salar
2016 年 2 月 25 日
They were also empty. M files are attached. Thank you so much!
Stephen23
2016 年 2 月 25 日
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).
Salar
2016 年 2 月 25 日
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.
Salar
2016 年 2 月 25 日
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
Salar
2016 年 2 月 25 日
oh ok , thank you!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
