merging a double matrix within a cell array

Hello,
I can't find the solution of merging a double matrix within a cell array:
A matrix m x n, C, where two contiguous elements within a
C=[1 5 3 7]
[2 6 4 8]
And in the Cell matrix D: D {1,:}= column headers D{:,1}= row headers
and therefore when merging C within D, each element in the cell will have two contiguous values from C, for example:
D {2,2} = [1 5] %first row and first two columns of C
D{3,4} = [4 8] %second row and last two columns of C
I am just trying to find a way to automate the merging.
Any input would be appreciated!

 Accepted Answer

T = mat2cell(C, 2 * ones(1, n/2), ones(1, m));
if size(D,1) < m; D{m,1} = {}; end %extend cell array if need be
if size(D,2) < n; D{1,n} = {}; end %extend cell array if need be
D(2:2+m-1, 2:2+n-1) = T;

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!