Hello, I have two matrices of different sizes and I want to find the indices for the elements in the bigger matrix that match with those in the smaller one, allowing for repetition?
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Alaa Hameed
 el 20 de En. de 2017
  
    
    
    
    
    Comentada: Alaa Hameed
 el 20 de En. de 2017
            Hello, I have two matrices of different sizes and I want to find the indices for the elements in the bigger matrix that match with those in the smaller one, allowing for repetition? A=[1 1 1 2 3 5 5 1 ] B=[1 2 5] I want to get C=[1 2 3 4 6 7 8]
1 comentario
  Jorge Mario Guerra González
      
 el 20 de En. de 2017
				For the forum order, try to summarize the title of the post into a shorter sentence. Check the answer I give you below.
Respuesta aceptada
  Image Analyst
      
      
 el 20 de En. de 2017
        Use ismember():
A=[1 1 1 2 3 5 5 1 ] 
B=[1 2 5] 
% Want C=[1 2 3 4 6 7 8]
[inA, inB] = ismember(A, B)
C = find(inA)
2 comentarios
  Jorge Mario Guerra González
      
 el 20 de En. de 2017
				yes, this is what I meant with a smarter way of doing it.
Más respuestas (1)
  Jorge Mario Guerra González
      
 el 20 de En. de 2017
        Hello Alaa, I believe the way to do it is just comparing array A with each element of B using find function. There must be a smarter way to it, but this example gives the output you want.
A=[1 1 1 2 3 5 5 1];
B=[1 2 5];
result=[];
for i=1:length(B)
  C=find(A==B(i));
  result=[result C]; %#ok<AGROW>
end
result=sort(result);
disp(result);
I hope it works for you
0 comentarios
Ver también
Categorías
				Más información sobre Operators and Elementary Operations en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

