Filter löschen
Filter löschen

How can I extract not zero numbers from a matrix?

1 Ansicht (letzte 30 Tage)
Simo
Simo am 24 Jun. 2014
Kommentiert: Image Analyst am 24 Jun. 2014
I have a matrix, like
M=[0 1 2 5 2 3 0 0 0 0 4 3 2 5 0 0 0 4 3 2 ...];
I'd like to extract n separated matrices, like:
A=[ 1 2 5 2 3];
B=[ 4 3 2 5];
C=[ 4 3 2];
or a matrix n Columns or rows, like
M=[A;B;C]
  1 Kommentar
Image Analyst
Image Analyst am 24 Jun. 2014
You cannot have an M like that because A, B, and C have different numbers of columns.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 24 Jun. 2014
M=[0 1 2 5 2 3 0 0 0 0 4 3 2 5 0 0 0 4 3 2 ];
idx=[0 M~=0 0];
ii1=strfind(idx,[0 1]);
ii2=strfind(idx,[1 0])-1;
out=arrayfun(@(ii,jj) M(ii:jj),ii1,ii2,'un',0);
celldisp(out)

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by