How extract sub matrix without zeros from a big matrix
Show older comments
Hello everybody ... I have a big matrix [N] as shown in Figure. I need to extract sub-matrix from [N]: [A],[B],[C],[D], [E] .... The end of each row can contain some zeros (red part). So I need to extract these sub-matrix. If [A] has the same number of columns of [E], I have to combine them in a unique matrix ([F]=[A];[E]). [N] does not contain zeros in the white part. Could you help me with this problem? Thank you very much for your time

2 Comments
KL
on 3 Nov 2017
what do you mean by big matrix? be specific? what's the size? How do you identify A,B,C,D and E within N? How are they distributed across N's rows?
gianluca scutiero
on 3 Nov 2017
Accepted Answer
More Answers (2)
Guillaume
on 3 Nov 2017
If I understood correctly:
%N: your big matrix
rowswithnozeros = cellfun(@(row) nonzeros(row).', num2cell(N, 2), 'UniformOutput', false);
rowlength = cellfun(@numel, rowswithnozeros);
[~, ~, subs] = unique(rowlength, 'stable'); %s'stable' optional
matriceswithnozeros = accumarray(subs, (1:numel(rowswithnozeros))', [], @(rows) {vertcat(rowswithnozeros{rows})});
3 Comments
gianluca scutiero
on 3 Nov 2017
Guillaume
on 3 Nov 2017
"But is there the possibility to join the rows generated with the same numbers of columns ?"
That's the whole purpose of the last three lines.
gianluca scutiero
on 3 Nov 2017
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!