How to concatenate elements in cell array to a vector?

1 view (last 30 days)
I have cell arrays with different size & I want to concatenate all the elements in the cell to become a vector. How to do that?
This is my code:
% Create cell arrays
Q= [23 34; 44 55; 56 71; 63 49; 71 30];
B= [12 13; 10 99];
C{1}=Q;
C{2}=B;
% Convert cells to vector
nCol = cellfun('size', C, 1);
nRow = numel(C);
No_of_elements = cellfun(@numel, C);
P = No_of_elements(1,1) + No_of_elements(1,2);
Output = NaN(1,P); % Size vector
for iC = 1:numel(C)
idx = (iC - 1) + 1;
Output(1:P(iC), idx:idx+1) = C{iC};
end
I want the final output to become, Output = [23 34 44 55 56 71 63 49 71 30 12 13 10 99]

Accepted Answer

Paul
Paul on 3 Aug 2022
Q= [23 34; 44 55; 56 71; 63 49; 71 30];
B= [12 13; 10 99];
C{1}=Q;
C{2}=B;
Output = reshape(vertcat(C{:}).',1,[])
Output = 1×14
23 34 44 55 56 71 63 49 71 30 12 13 10 99
  3 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!