matching values by comparing different length vectors

21 visualizaciones (últimos 30 días)
chiara.zip
chiara.zip el 19 de Jun. de 2017
Respondida: Jan el 20 de Jun. de 2017
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
chiara.zip
chiara.zip el 20 de Jun. de 2017
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)

Image Analyst
Image Analyst el 19 de Jun. de 2017
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

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by