ismember(A,B,'rows') indexing

12 visualizaciones (últimos 30 días)
Waqar Ali Memon
Waqar Ali Memon el 11 de Jul. de 2019
Comentada: Waqar Ali Memon el 14 de Jul. de 2019
Hello everyone,
I would like to compare two cells, want to see if element of A is a member of B, If yes then it should return 1 otherwise 0.
Size of A is 189x1 and Size of B is 108x1.
I used:
C = ismember(A,B,'rows');
It returned logical o,1. With Size of 189x1, Perfect.
But now. I want Values of B (108x1) sorted same like C with Size 189x1.
Any help would be appreciated.
Thank you.
Regard,s
Waqar Ali
  4 comentarios
dpb
dpb el 11 de Jul. de 2019
Well, that's easy enough -- add 81 elements on the end. The question is, what are the new elements to contain? I don't see there's any correlation between the two requests.
Waqar Ali Memon
Waqar Ali Memon el 11 de Jul. de 2019
Hello dpb,
Adding 81 elements will make it wrong. Since i require Output like following:
For suppose, Cell1 contains following elements.
Cell1: Had 2 columns, I have concentrated them and shows as follows.
  1. 'ADSµSOIC8'
  2. 'AVX0603'
  3. 'ELN'
  4. 'ELNH10'
  5. 'EPC'
  6. 'EPC0603'
  7. 'FAGDO214AA'
  8. 'FAGDO214AA (SMB)'
  9. 'FAGDO214AB'
  10. 'FAGDO214AC'
  11. 'FAGSMA'
  12. 'FAGSOD123W'
  13. 'FAGSOD128'
  14. 'FSLLQFP-64 ePAD'
  15. 'FSLLQFP80-ePad'
Cell2: Had 3 columns, I have concentrated first 2 columns and shows as follows but Cell2 only contains 10 random elements ( In this example only).
  1. 'ADSµSOIC8' '1'
  2. 'AVX0603' '3'
  3. 'ELN' '2'
  4. 'EPC' '2'
  5. 'EPC0603' '3'
  6. 'FAGDO214AA' '10'
  7. 'FAGDO214AC' '1'
  8. 'FAGSOD123W' '5'
  9. 'FAGSOD128' '2'
  10. 'FSLLQFP-64 ePAD' '1'
I want to create new cell3: That should contain all elements of Cell1: (I have already created).
Now I would like to add 2nd Column in Cell1 which should show values of Cell2 which should be in same order of Cell1 i.e. 15x1.
i.e. output should be like:
Required Output:
  1. 'ADSµSOIC8' '1'
  2. 'AVX0603' '3'
  3. 'ELN' '2'
  4. 'ELNH10' '0'
  5. 'EPC' '2'
  6. 'EPC0603' '3'
  7. 'FAGDO214AA' '10'
  8. 'FAGDO214AA (SMB)' '0'
  9. 'FAGDO214AB' '0'
  10. 'FAGDO214AC' '1'
  11. 'FAGSMA' '0'
  12. 'FAGSOD123W' '5'
  13. 'FAGSOD128' '2'
  14. 'FSLLQFP-64 ePAD' '1'
  15. 'FSLLQFP80-ePad' '0'
This is the best explanation i guess :-)
Thank you everyone.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 11 de Jul. de 2019
Editada: Stephen23 el 11 de Jul. de 2019
>> one = {'ADSµSOIC8';'AVX0603';'ELN';'ELNH10';'EPC';'EPC0603';'FAGDO214AA';'FAGDO214AA (SMB)';'FAGDO214AB';'FAGDO214AC';'FAGSMA';'FAGSOD123W';'FAGSOD128';'FSLLQFP-64 ePAD';'FSLLQFP80-ePad'}
one =
'ADSµSOIC8'
'AVX0603'
'ELN'
'ELNH10'
'EPC'
'EPC0603'
'FAGDO214AA'
'FAGDO214AA (SMB)'
'FAGDO214AB'
'FAGDO214AC'
'FAGSMA'
'FAGSOD123W'
'FAGSOD128'
'FSLLQFP-64 ePAD'
'FSLLQFP80-ePad'
>> two = {'ADSµSOIC8','1';'AVX0603','3';'ELN','2';'EPC','2';'EPC0603','3';'FAGDO214AA','10';'FAGDO214AC','1';'FAGSOD123W','5';'FAGSOD128','2';'FSLLQFP-64 ePAD','1'}
two =
'ADSµSOIC8' '1'
'AVX0603' '3'
'ELN' '2'
'EPC' '2'
'EPC0603' '3'
'FAGDO214AA' '10'
'FAGDO214AC' '1'
'FAGSOD123W' '5'
'FAGSOD128' '2'
'FSLLQFP-64 ePAD' '1'
>> [X,Y] = ismember(one,two);
>> out = one;
>> out(:,2) = {'0'};
>> out(X,2) = two(Y(X),2)
out =
'ADSµSOIC8' '1'
'AVX0603' '3'
'ELN' '2'
'ELNH10' '0'
'EPC' '2'
'EPC0603' '3'
'FAGDO214AA' '10'
'FAGDO214AA (SMB)' '0'
'FAGDO214AB' '0'
'FAGDO214AC' '1'
'FAGSMA' '0'
'FAGSOD123W' '5'
'FAGSOD128' '2'
'FSLLQFP-64 ePAD' '1'
'FSLLQFP80-ePad' '0'
  4 comentarios
Waqar Ali Memon
Waqar Ali Memon el 11 de Jul. de 2019
Now it is working, i restarted the program.
Thank you so much :-)
Guillaume
Guillaume el 11 de Jul. de 2019
Editada: Guillaume el 11 de Jul. de 2019
One must wonder why an answer is accepted to then say it doesn't work.
As I've pointed out in my answer, you haven't given us enough details to know what you're doing exactly, so it's likely that no answer will work straight out of the box, but you should be able to work it out from there.
As said, you need to use the 2nd return value of ismember.
I'd recommend you not use X and Y as variable names but soemthing more meaningful

Iniciar sesión para comentar.

Más respuestas (2)

joe
joe el 11 de Jul. de 2019
function result = compareMatrices( A,B)
result = zeros(size(A,1),size(B,2)); % generate matrix with the same size as the A
for i=1:numel(A) % this loop checkes the existens of all elements of A in B
[~,index]=ismember(A{i,1},B);
if index~=0 % if any element of A exists in B
result{i,1} = 1; % set 1 in the same position where the existens detected
end
end
end
  5 comentarios
joe
joe el 11 de Jul. de 2019
if you have matrices with elements of deferent types
try to call the function like this: compareMatrices(string(A), string(B))
Waqar Ali Memon
Waqar Ali Memon el 14 de Jul. de 2019
Thanks joe for yOur time :-)

Iniciar sesión para comentar.


Guillaume
Guillaume el 11 de Jul. de 2019
If the two cell arrays don't have the same number of columns, you're obviously not using ismember(A, B, 'rows') but something slightly more complex. I'm taking a guess here.
You also haven't said what needs to go in the result, when the row of A is not found in B.
In any case, you just have to use the 2nd output of ismember
[isfound, where] = ismember(A(:, 1), B(:, 1)); %compare column 1 of A and column 1 of B
C = [A(isfound), B(where(isfound), 3)]; %get rows of A found in B together with the matching value of column 3 of B
Adapt as necessary.
  1 comentario
Waqar Ali Memon
Waqar Ali Memon el 11 de Jul. de 2019
This returned again the actual size. Thanks Man, Problem solved :-)

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by