Non Zero element appear first with keeping the same size of the matrix?
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Rubel Ahmed
 el 5 de Nov. de 2019
  
    
    
    
    
    Comentada: Rubel Ahmed
 el 5 de Nov. de 2019
            A  = [1 0 2 3;2 0 0 5; 3 0 0 1; 0 0 1 5];
How The matrix will be transformed like this (No-zero element will come first, row wise)
A = [1 2 3 0; 2 5 0 0; 3 1 0 0; 1 5 0 0] 
0 comentarios
Respuesta aceptada
  JESUS DAVID ARIZA ROYETH
      
 el 5 de Nov. de 2019
        solution :
A  = [1 0 2 3;2 0 0 5; 3 0 0 1; 0 0 1 5];
A(A==0)=nan;
A=sort(A,2);
A(isnan(A))=0;
disp(A)
2 comentarios
Más respuestas (1)
  Richard Brown
      
 el 5 de Nov. de 2019
        here's a really naive way to do it:
[m, n] = size(A);
for i = 1:m
    k = find(A(i, :))
    A(i, :) = [A(i, k), zeros(1, n - numel(k))]
end
I'm sure there's a more fancy vectorised method, but this will do the job.
2 comentarios
Ver también
Categorías
				Más información sobre Dates and Time en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


