Compact way to horizontally concatenate a cell array without the first columns of the cells

25 views (last 30 days)
Hi, I have two matrices A and B that are "stored" inside a cell array C.
Just manipulating the cell array C (and not matrices A and B), I would like to horizontally concatenate the two cells of C in a compact way, with the condition of not taking the first column of both cells.
Here my example:
% input
A = [2 4 6
1 3 8
6 9 1
6 1 1
5 5 2
3 7 8
9 2 4]
B = [8 3 5
9 9 5
6 3 1
7 1 2
4 8 7
2 2 4
5 7 3]
% creation of the cell array C
C{1} = A;
C{2} = B;
% horizontal concatenation of the C's cells
D = horzcat(C{:})
% result - just using horzcat(C{:}), the first columns of A and B are present
D =
2 4 6 8 3 5
1 3 8 9 9 5
6 9 1 6 3 1
6 1 1 7 1 2
5 5 2 4 8 7
3 7 8 2 2 4
9 2 4 5 7 3
% desired output - where the first columns of A and B are absent
D =
4 6 3 5
3 8 9 5
9 1 3 1
1 1 1 2
5 2 8 7
7 8 2 4
2 4 7 3
% Note: the following naive attempt gives me an error
>> horzcat(C{:}(:,2:3))
Expected one output from a curly brace or dot indexing expression, but there were 2 results.
Is there any compact way to get my desired output,
  • just manipulating C (and not A and B) and
  • possibly still using horzcat (or similar built-in functions), like/similarly to horzcat(C{:}) ?

Accepted Answer

Stephen23
Stephen23 on 7 Apr 2022
Edited: Stephen23 on 7 Apr 2022
It rather depends on what you mean by "compact".
C = {[2,4,6;1,3,8;6,9,1;6,1,1;5,5,2;3,7,8;9,2,4],[8,3,5;9,9,5;6,3,1;7,1,2;4,8,7;2,2,4;5,7,3]}
C = 1×2 cell array
{7×3 double} {7×3 double}
Method one, probably simplest and clearest:
D = horzcat(C{:});
D(:,1:3:end) = []
D = 7×4
4 6 3 5 3 8 9 5 9 1 3 1 1 1 1 2 5 2 8 7 7 8 2 4 2 4 7 3
Method two: CELLFUN and CELL2MAT:
D = cell2mat(cellfun(@(m)m(:,2:end),C,'uni',0))
D = 7×4
4 6 3 5 3 8 9 5 9 1 3 1 1 1 1 2 5 2 8 7 7 8 2 4 2 4 7 3
Method three: CELLFUN and HORZCAT (probably more efficient than CELL2MAT):
D = cellfun(@(m)m(:,2:end),C,'uni',0);
D = horzcat(D{:})
D = 7×4
4 6 3 5 3 8 9 5 9 1 3 1 1 1 1 2 5 2 8 7 7 8 2 4 2 4 7 3
  1 Comment
Sim
Sim on 7 Apr 2022
Cool!! thanks a lot @Stephen!!
Works perfectly for several (and larger) matrices!
% input
A = [2 4 6 1
1 3 8 3
6 9 1 5
6 1 1 6
5 5 2 2
3 7 8 1
9 2 4 6];
B = [8 3 5 1
9 9 5 2
6 3 1 8
7 1 2 9
4 8 7 9
2 2 4 6
5 7 3 7];
W = [5 6 8 5
3 4 7 1
6 3 1 8
9 7 3 9
2 2 3 1
3 8 9 1
5 5 8 2];
Z = [6 4 2 3
1 1 1 9
9 8 9 4
2 1 4 5
9 3 1 2
8 7 7 9
6 5 3 7];
% creation of the cell array C
C{1} = A;
C{2} = B;
C{3} = W;
C{4} = Z;
% horizontally concatenation of the C's cells
D = cellfun(@(m)m(:,2:end),C,'uni',0);
D = horzcat(D{:})
% result with Method three
D =
4 6 1 3 5 1 6 8 5 4 2 3
3 8 3 9 5 2 4 7 1 1 1 9
9 1 5 3 1 8 3 1 8 8 9 4
1 1 6 1 2 9 7 3 9 1 4 5
5 2 2 8 7 9 2 3 1 3 1 2
7 8 1 2 4 6 8 9 1 7 7 9
2 4 6 7 3 7 5 8 2 5 3 7

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!