matching values by comparing different length vectors

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 comentarios

Jan
Jan el 19 de Jun. de 2017
Editada: Jan el 19 de Jun. de 2017
"they do not seem working"? Do you get an error message?
It is not even Matlab, who has problems with "if A=B", when A and B have a different size. I cannot imagine, what you want as output also. Please post, what you want to achive for the given inputs.
I would expect an output vector like C=[100 20 200 300 400];, which includes B1 values when A=B and A1 values when A<>B
Jan
Jan el 20 de Jun. de 2017
Editada: Jan el 20 de Jun. de 2017
This explanation still contains the term "A=B", which is not defined. "I would expect an output vector like" sounds too vague for a unique definition also.
What do you mean with "this explanation still contains the term A=B that is not defined"? This is a condition of my if statement: if A=B, write B1; if A<>B, write A1
A = [1; 2; 3];
B = [1, 3, 8, 9];
What does "if A=B" mean then? This is neither clear nor mathematically defined and in consequence there is no operator for this. You do know, what you expect, but I cannot and should not guess it. To be exact "A=B" assigns the vector B to the variable A overwriting the former contents of A.
ismember has been suggested already. This is most likely useful, but it has not been uniquely explained, why C=[100 20 200 300 400] is the result you expect.
Yes, you are right. My output vector should contain values in B1 when A is included in B and values in A1 when A is not included in B. My problem with the ismember function is that it is a logical function and it doesn't allow for a numeric vector C. Maybe it could be used in combination with other functions, but I cannot see how
"My output vector should contain values in B1" <== which values in B1. There are lots of them.
" when A is included in B" <== when WHICH values of A are in B? All the values of A, or just at least one of the values of A need to be in B?
" and values in A1" <== again, WHICH values of A1? All of them? Some of them? Which ones?
"when A is not included in B" How much of A? All of A needs to be not included in B? If so, in the same order in adjacent elements, or can the values of A be scattered throughout B? Or just some/any of the values of A need to be in B?
Please, please, please be precise in your wording. I'm about to give up here.
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 el 20 de Jun. de 2017
Editada: Jan el 20 de Jun. de 2017
@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 ! :-)

Iniciar sesión para comentar.

Respuestas (2)

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
Jan
Jan el 20 de Jun. de 2017
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?

Categorías

Etiquetas

Preguntada:

el 19 de Jun. de 2017

Respondida:

Jan
el 20 de Jun. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by