필터 지우기
필터 지우기

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

조회 수: 23 (최근 30일)
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{:}) ?

채택된 답변

Stephen23
Stephen23 2022년 4월 7일
편집: Stephen23 2022년 4월 7일
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
Sim
Sim 2022년 4월 7일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by