Effacer les filtres
Effacer les filtres

Select matrices with nonzero rows from a bigger matrix ?

1 vue (au cours des 30 derniers jours)
Geraldine Ejimadu
Geraldine Ejimadu le 26 Avr 2020
Commenté : dpb le 27 Avr 2020
Hello guys!
I am struggling with this problem:
I have a big matrix that is made of few zeros raws and many nonzero raws.
This is a fake simple example to explain, because in reality I am working with big data :(:
B= [0 0 0 0;
0 0 0 0;
0 0 0 0;
0 0 0 0 ;
1 1 2 3;
0 0 0 3;
1 1 1 0;
4 4 4 4;
0 0 0 0 ;
0 0 0 0;
0 0 0 0 ;
0 0 0 8;
0 0 1 0;
0 1 1 0;
0 0 0 0;
0 0 0 0 ];
The zero rows in this matrix, indicate that the signal stopped and I just need to analyse the non zero raws in chunks.
Therefore would like to obtain:
chunk_1=[1 1 2 3;0 0 0 3;1 1 1 0;4 4 4 4]
chunk_2=[0 0 0 8;0 0 1 0;0 1 1 0;]
As you can see the chunks don't have to have the same amount of raws, since I analyse them individually, and the zero chunks also don't have the same number of rows.
I hope you guys have more knowledge than ve and could help me!
Thanks in advance !

Réponse acceptée

Ameer Hamza
Ameer Hamza le 26 Avr 2020
Modifié(e) : Ameer Hamza le 26 Avr 2020
This nice little function from the image processing toolbox (bwconncomp) will be very useful here.
B = [0 0 0 0;0 0 0 0;0 0 0 0;0 0 0 0 ;1 1 2 3;0 0 0 3;1 1 1 0;4 4 4 4; 0 0 0 0 ;0 0 0 0;0 0 0 0 ;0 0 0 8;0 0 1 0;0 1 1 0;0 0 0 0;0 0 0 0 ];
mask = sum(B, 2)~=0;
comps = bwconncomp(mask);
compMaskList = comps.PixelIdxList;
B_components = cell(numel(compMaskList), 1);
for i=1:numel(compMaskList)
B_components{i} = B(compMaskList{i}, :);
end
This shorter version is equivalent to the above code
comps = bwconncomp(sum(B, 2)~=0);
B_comps = cellfun(@(x) {B(x, :)}, comps.PixelIdxList);
  5 commentaires
Ameer Hamza
Ameer Hamza le 26 Avr 2020
If you run 'sum(B, 2)~=0', you get
>> sum(B, 2)~=0
ans =
16×1 logical array
0
0
0
0
1
1
1
1
0
0
0
1
1
1
0
0
So bwconncomp detects that there are two groups of 1s and gives back their row number.
Geraldine Ejimadu
Geraldine Ejimadu le 27 Avr 2020
It works, thank you so much!!!

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 26 Avr 2020
Any number of these kinds of Q? on Answers -- under "runs" or any number of other terms...but, it's pretty simple to code from scratch--
d=diff([false;any(B,2)]); % locations with any observations--T
ix=[find(d==1) find(d==-1)]; % find start, end of sections 0->1, 1->0
C=arrayfun(@(i) B(ix(i,1):ix(i,2),:),1:size(ix,1),'UniformOutput',false); % cell array of contents by group
  4 commentaires
Geraldine Ejimadu
Geraldine Ejimadu le 27 Avr 2020
Thank you!! This is really helpful!
dpb
dpb le 27 Avr 2020
Very common type problem and typical use of diff for pattern-matching. "Trick" worth knowing; can adapt to many situations with thought.
May not always have Image Processing TB (as I don't) so need to be able to do oneself...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by