Extract a row from a 2D matrix if any of its element matches with elements in other 1D matrix
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Sachin Shinde
 el 3 de Jul. de 2020
  
    
    
    
    
    Comentada: Sachin Shinde
 el 3 de Jul. de 2020
            I have 2D matrxi A (size: 10000 x 3) and 1D matrix B (size: 5000 x 1)
If one elemnt of row in A matches with any element in B, then that rwo sfould be separated out. Suppose there are N such rows then I want the output as a matrix C (size: N x 3),  (which is a subset of A).
I am using the code:
counter = 0
for i = 1:length(A)    
    if sum(ismember(A(i,:),B)) >= 1
        counter = counter + 1;
        C(counter,:) = A(i,:);
    end
end
This gives me the output I want, but it takes huge time especially on very large sized matrices.
Any one/two liner replacment to speed up the code?
Thanks.
2 comentarios
Respuesta aceptada
  Gaurav Aggarwal
      
 el 3 de Jul. de 2020
        
      Editada: Gaurav Aggarwal
      
 el 3 de Jul. de 2020
  
      Hi Sachin,
Can you check if this works for you?
C = A(any(ismember(A,B),2),:);
Thanks.
Más respuestas (1)
  Gifari Zulkarnaen
      
 el 3 de Jul. de 2020
        Try this:
idx = [];
for i = 1:size(A,1)
    if any(ismember(A(i,:),B))
        idx = [idx i];
    end
end
C = A(idx,:);
Ver también
Categorías
				Más información sobre Get Started with MATLAB 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!


