Hope to gather last rows from each submatrices

I hope to gather last lines from each submatrix.
- I have 17 x 20 submatrices in matrix name A.
- Each submatrices have different number of lines, but same number of columns (total 7 columns, all)
- I tried to generate a file, made up of only last rows of each submatrices. My target file's from will be
M = [column1 column2 column3 column4 column5 column6 column7]
% made up of last rows of each submatrices, unknown number of
lines, 7 columns
- So I tried
for x_cc = 1:20
for y_cc = 1:17
M = A{x_cc, y_cc}(end,:);
end
end
- But it is not working, giving the error Subscript indices must either be real positive integers or logicals.
- Should I need to define the size first? What operation should be done? or what commands are useful? I tried cellfun, but not sure how can I use here.
- Need any help to solve this situation. Thanks~!

5 Comments

Did you accidentally define a variable named "end" ?
In the output, should it first go down the first column of submatrices of A, or across the first row of submatrices of A ?
Thanks Walter,
1) No, I used "end" because I hope to gather only last rows, so I used (end,:)
2) I could not clearly understand the 2nd question.. I hope to "stack up" the last rows of the each submatrices, in the matrix M.
Stack in what order? If your A is
P Q
R S
then do you want the last lines in the order
P
Q
R
S
or in the order
P
R
Q
S
Order is not important, hope to just stack the last lines of each cell array of matrix.
I have
A =
[0x7 double] [10x7 double] [ 9x7 double]
[4x7 double] [ 5x7 double] [ 6x7 double]
....
Hope to choice only last lines from each arrays, and stack in the single matrix.
There are empty cells are exists, hope to just get 0 in that case.

Sign in to comment.

 Accepted Answer

AT = A(~cellfun(@isempty,A));
M = cellfun( @(C) C(end,:), AT);

2 Comments

Great thanks~! I really need to study cellfun more~!
If you are certain of not having empty submatrices, you can reduce to
M = cellfun( @(C) C(end,:), A(:));

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!