Indexing with two matrices

1 回表示 (過去 30 日間)
Tchilabalo
Tchilabalo 2020 年 1 月 21 日
コメント済み: Tchilabalo 2020 年 1 月 23 日
I have two matrices A(1000 by 100) and B(10 by 100).
A=
1 4 20
3 5 15
2 1 24
7 9 42
B=
3 4 15
7 1 42
I want to return a matrix C such that, for each column of B i find the indexes in the coresponding column in A.
For this case:
C=
2 1 2
4 3 4
I have tried the code below, but i am not getting what I want.
for i=1:size(A,2)
C(:,i)=intersect(A(i),B(i));
end

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 1 月 21 日
編集済み: KALYAN ACHARJYA 2020 年 1 月 21 日
A=[1 4 20
3 5 15
2 1 24
7 9 42];
B=[3 4 15
7 1 42];
[r1,c1]=size(B);
c=zeros(r1,c1);
for i=1:r1
for j=1:c1
r=find(B(i,j)==A(:,j))
c(i,j)=r
end
end
c
#
c =
2 1 2
4 3 4
  2 件のコメント
Tchilabalo
Tchilabalo 2020 年 1 月 21 日
Thanks Kalyan for your answer. The code is working but i have on special case: what if i have one value repeated multiple times in a column? Here is an example:
A=[1 4 20
3 5 15
2 1 24
2 5 15
3 4 15
7 9 42];
B=[3 4 15
7 1 42];
[r1,c1]=size(B);
c=zeros(r1,c1);
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 1 月 21 日
編集済み: KALYAN ACHARJYA 2020 年 1 月 21 日
but i have on special case: what if i have one value repeated multiple times in a column
The logic, you have to find out, we are here to help you to implementation in MATLAB.

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 1 月 22 日
>> [C,~] = find(bsxfun(@eq,permute(A,[1,3,2]),permute(B,[3,1,2])));
>> C = reshape(C,size(B))
C =
2 1 2
4 3 4
  1 件のコメント
Tchilabalo
Tchilabalo 2020 年 1 月 23 日
Thanks Stephen for your answer. It is working now.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by