matching values by comparing different length vectors

조회 수: 15 (최근 30일)
chiara.zip
chiara.zip 2017년 6월 19일
답변: Jan 2017년 6월 20일
Hi everyone,
I have 4 columns of data: A=[1 2 3]'; A1=[10 20 30]'; B=[1 3 8 9]; B1=[100 200 300 400];
I would like to create an output vector with data in B1 if A=B and in A1 if A and B are different.
I tried different methods/functions (loop, ismember, find, etc...), but they do not seem working because the vectors have different lengths.
Any help?
Thank you in advance.
  댓글 수: 9
chiara.zip
chiara.zip 2017년 6월 20일
The statement would be: if A==B, then write corresponding B1 in vector C, else if A<>B, then write corresponding A1 in C. Hence, I expect to have the first value in C equal to 100 because the first value in A is equal to the first value in B; the second value in C should be 20, because the second values in B and A are different, and so on....
Jan
Jan 2017년 6월 20일
편집: Jan 2017년 6월 20일
@Chiara: Fine.
"the second values in B and A are different": Then "A<>B" means: ~ismember(A(i), B)
The clarity can still be improved:
"A==B, then write corresponding B1": what does "corresponding" mean exactly then? Has "corresponding" the same index for the case "A==B" and "A<>B"?
Note that nobody but you know, how the operators are defined, if you have invented them until you share the definition. "A==B" for vectors of different sizes is a meaningful as "A ??% B". It looks like you have an idea what your "A==B" means, but for an implementation in Matlab you have to explain this. Simply repeating "A==B" does not clarify the meaning, even if it is obvious for yourself.
What is the wanted output for:
A = [1 4 5]';
A1 = [10 20 30]';
B = [1 3 8 9];
B1 = [100 200 300 400];
? Does it matter that the first missing value of A is greater than the 2nd element of B? Is this an "insertion sort"? The order and relative values of the elements have not been mentioned yet, but they might matter.
Chiara, do not let this discussion discourage you. Programming means, that the wanted steps have to be cleared in a mathematical sense at first. This is a serious task and can be really hard. You will obtain more experiences with this, if you proceed to learn. So keep on trying ! :-)

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

답변 (2개)

Image Analyst
Image Analyst 2017년 6월 19일
By chance are you thinking of setdiff() and intersect()?
A=[1 2 3];
B=[1 3 8 9];
differentValues = unique([setdiff(A, B),setdiff(B, A)])
commonValues = intersect(A, B)
Results:
differentValues =
2 8 9
commonValues =
1 3
  댓글 수: 1
chiara.zip
chiara.zip 2017년 6월 20일
Yes, thanks, but it is not the result I want to have

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


Jan
Jan 2017년 6월 20일
Another guess:
A = [1 4 5]; % Ignore the transpose here
A1 = [10 20 30];
B = [1 3 8 9];
B1 = [100 200 300 400];
AB = union(A, B);
C = NaN(size(AB));
[iA, pA] = ismember(AB, A);
C(iA) = A1(pA(iA));
[iB, pB] = ismember(AB, B);
C(iB) = B1(pB(iB));
This is: The elements of A and B are joined to a sorted unique vector AB. For all elements of AB occur in A, but not in B, the output C is set to the value of A1, which corresponds to the index of the element in A. For all elements of AB, which occur in B (and maybe also in A), the output C is set to the corresponding element of B1, which has the same index as the element in B.
This cannot match the explanation "A==B" or "A<>B" (whereby the latter might mean "A~=B" to be more matlabish?). But your example data are reproduced.
For
A = [1 4 5];
A1 = [10 20 30];
B = [1 3 8 9];
B1 = [100 200 300 400];
we get
C = [100, 200, 20, 30, 300, 400]
Is this wanted?

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by