Compact way to horizontally concatenate a cell array without the first columns of the cells
23 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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{:}) ?
0 commentaires
Réponse acceptée
Stephen23
le 7 Avr 2022
Modifié(e) : Stephen23
le 7 Avr 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]}
Method one, probably simplest and clearest:
D = horzcat(C{:});
D(:,1:3:end) = []
Method two: CELLFUN and CELL2MAT:
D = cell2mat(cellfun(@(m)m(:,2:end),C,'uni',0))
Method three: CELLFUN and HORZCAT (probably more efficient than CELL2MAT):
D = cellfun(@(m)m(:,2:end),C,'uni',0);
D = horzcat(D{:})
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays 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!