Error when using find function

There are 2 matrix, A and B
A=[1
2]
B=[1
2
2]
% Check if the element in A is repeated at B
Check = find (B(:,1)==A)
%Error
%Unable to perform assignment because the left and right sides have a different number of elements.
Check = find (B(1:2,1)==A) %working fine
I need the value 2 in A to be compared with both value 2 in B. I was only manage to compre 2 element with 2 element. Is there any methos to achieve comparing array with 2 element with array with 3 element by using "find" fucntion? Thanks in advance.

回答 (2 件)

Paul
Paul 2021 年 1 月 1 日

0 投票

Maybe you want to start with ismember?
doc ismember
Walter Roberson
Walter Roberson 2021 年 1 月 1 日

0 投票

%Unable to perform assignment because the left and right sides have a different number of elements.
That would give
Matrix dimensions must agree.
I need the value 2 in A to be compared with both value 2 in B.
find(B == A(2))
Is there any methos to achieve comparing array with 2 element with array with 3 element by using "find" fucntion?
>> [R,C] = find(B == A.')
R =
1
2
3
C =
1
2
2
Read that as B(1) = A(1), B(2) = A(2), B(3) = A(2)
But I have to wonder whether you really want to use find. I can't tell what you are really aiming to do with the information. I suggest you consider
>> [wasthere, idx] = ismember(B,A)
wasthere =
3×1 logical array
1
1
1
idx =
1
2
2
Or perhaps ismember(A,B) depending what you are trying to do.

5 件のコメント

Chau Chin Haw
Chau Chin Haw 2021 年 1 月 1 日
Sorry i think you have misunderstood my question. What i want to achive is comparing A with B despite the different in number of element
Walter Roberson
Walter Roberson 2021 年 1 月 1 日
What I posted does compare A with B despite the difference in the number of elements.
You specifically said that the value 2 in A is to be compared to both value 2 in B. find(B == A.') compares each element in B to each element in A and so satisfies that requirement.
Are you trying to say that if B is longer than A, you want the last element of A to be effectively repeated?
What output would you want for
A = [4; 2]
B = [1; 2; 2; 3; 4]
Chau Chin Haw
Chau Chin Haw 2021 年 1 月 2 日
I want to compare all the element in A with B
A=[1
2]
B=[1
2
2]
Check = find (B(:,1)==A)
The output should be
Check = [1
1
1]
A(1) compared with B(1)
A(2) compared with B(2)
A(2) once again compared with third element of B, 2
Paul
Paul 2021 年 1 月 2 日
As has been stated in two different answers, the function ISMEMBER is what you need. Have you looked at its documentation? If so and its not satisfactory, why not?
Stephen23
Stephen23 2021 年 1 月 2 日
This is a very odd definition of comparing two vectors: "A(1) compared with B(1), A(2) compared with B(2), A(2) once again compared with third element of B". In any case, neither find nor ismember will do that unusual comparison, you will have to DIY.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2021 年 1 月 1 日

コメント済み:

2021 年 1 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by