Adding column in cell array before converting to a matrix.

I have a cell array with 23 X 1 doubles. Within each cell double there are two columns with thousands of rows.
Each of the 23 cells is representative of a sample in a project.
I need to convert this cell array into a matrix with all the data, but the final matrix needs to have an identifer with the cell name from the previous array (ex. 1,2,etc). The screenshot is an example of what I'm looking for within the first cell of the array. How do I add a column with values to one cell in a cell array?

 采纳的回答

Here are a couple of approaches. First lets create some fake data:
C = {randi(9,3,2),randi(9,2,2),randi(9,4,2)}
C = 1x3 cell array
{3x2 double} {2x2 double} {4x2 double}
C{:}
ans = 3x2
8 2 5 1 3 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 2x2
7 8 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 4x2
1 6 5 3 8 3 8 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Method one: CELLFUN and REPELEM:
X = cellfun(@height,C);
Y = 1:numel(X);
D = [vertcat(C{:}),repelem(Y(:),X,1)]
D = 9x3
8 2 1 5 1 1 3 1 1 7 8 2 1 2 2 1 6 3 5 3 3 8 3 3 8 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Method two: FOR-loop (modifies the original data):
for k = 1:numel(C)
C{k}(:,3) = k;
end
M = vertcat(C{:})
M = 9x3
8 2 1 5 1 1 3 1 1 7 8 2 1 2 2 1 6 3 5 3 3 8 3 3 8 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

2 个评论

This worked! Thank you very much.
@Emily: please remember to click the accept button if my answer helped you!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by