Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

extruct non zeros values and put into a short matrics

1 visualización (últimos 30 días)
Asl
Asl el 18 de Nov. de 2013
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
A =
0 0 0 0 5 0 0 8 0 0 0 0 8
0 0 0 5 0 0 0 8 0 0 0 0 9
0 0 0 5 0 0 0 7 0 0 0 0 8
0 0 0 0 5 0 0 0 8 0 0 0 9
0 0 0 6 0 0 0 8 0 0 0 0 9
0 0 0 5 0 0 0 7 0 0 0 0 8
0 0 0 8 0 0 0 8 0 0 0 0 9
I want to extruch a small matrics (with 3 columns) containing only non zeros values
Results:
5 8 8
5 8 9
5 7 8
5 8 9
6 8 9
5 7 8
8 8 9
ho I can do this smartly?
thanks
  1 comentario
Image Analyst
Image Analyst el 18 de Nov. de 2013
To do it smartly and robustly, you'd check to make sure that there are the same number of zeros in each row. If there aren't you'd need to decide how to handle that, for example, take the first or last three
threeValues = find(oneRow~= 0, 3, 'first'); % Could be empty, 1, 2, or 3 at most.
if length(threeValues) == 3 .... etc.

Respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 18 de Nov. de 2013
Editada: Azzi Abdelmalek el 18 de Nov. de 2013
out=cell2mat(arrayfun(@(x) A(x,A(x,:)~=0),(1:size(A,1))','un',0))
%or
n=size(A,1);
B=zeros(n,3);
for k=1:n
B(k,:)=A(k,A(k,:)~=0);
end
B
  2 comentarios
The Matlab Spot
The Matlab Spot el 18 de Nov. de 2013
this is good...
out=cell2mat(arrayfun(@(x) A(x,A(x,:)~=0),(1:size(A,1))','un',0))
Azzi Abdelmalek
Azzi Abdelmalek el 18 de Nov. de 2013
Not necessary, the second using for loop is slightly faster then arrayfun

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by