Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

extruct non zeros values and put into a short matrics

1 Ansicht (letzte 30 Tage)
Asl
Asl am 18 Nov. 2013
Geschlossen: MATLAB Answer Bot am 20 Aug. 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 Kommentar
Image Analyst
Image Analyst am 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.

Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 18 Nov. 2013
Bearbeitet: Azzi Abdelmalek am 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 Kommentare
The Matlab Spot
The Matlab Spot am 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 am 18 Nov. 2013
Not necessary, the second using for loop is slightly faster then arrayfun

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by