Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

extruct non zeros values and put into a short matrics

1 vue (au cours des 30 derniers jours)
Asl
Asl le 18 Nov 2013
Clôturé : MATLAB Answer Bot le 20 Août 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 commentaire
Image Analyst
Image Analyst le 18 Nov 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.

Réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 18 Nov 2013
Modifié(e) : Azzi Abdelmalek le 18 Nov 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 commentaires
The Matlab Spot
The Matlab Spot le 18 Nov 2013
this is good...
out=cell2mat(arrayfun(@(x) A(x,A(x,:)~=0),(1:size(A,1))','un',0))
Azzi Abdelmalek
Azzi Abdelmalek le 18 Nov 2013
Not necessary, the second using for loop is slightly faster then arrayfun

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by