# How to assign label from one cell array to data inside another cell array?

12 ビュー (過去 30 日間)
Susan 2020 年 7 月 10 日
コメント済み: Susan 2020 年 7 月 13 日
Hello everyone,
I have a 10*200 cell array C which each of these cells contains 1*1000 cell array.
I have another 10*200 cell array B which contains labels.
for example C{1,1} has label B{1,1}. I would like to assign label B{1,1} to each of 1000 cell array in cell C{1,1}. How can I do that? I would like to creat a function, and then apply this function to each cell array of C using cellfun. Any help would be greatly appreciated!
##### 2 件のコメント表示非表示 1 件の古いコメント
Susan 2020 年 7 月 10 日

Thanks for your reply. Sure, here is an example with small size
C = {ones(4,5), 2*ones(4,5), 3*ones(4,5); 4*ones(4,5), 5*ones(4,5), 6*ones(4,5)};
B = {7, 8, 9 ; 10, 11, 12};
I'm using the following code to put each column of the matrix in C{i,j} to one cell
E = cellfun(@(x) mat2cell(x,size(x,1),ones(1,size(x,2))), C, 'UniformOutput',false)
Now, E is a 2*3 cell array and E{1,1} is a 1*5 cell array. I would like to assign B{1,1}, i.e., 7 to each of these 1*5 cell arrays in E{1,1} and so on. In other words B{1,1} is the label of each of 5 cells in E{1,1}. Please let me know if it makes sense now.

サインインしてコメントする。

### 採用された回答

Fangjun Jiang 2020 年 7 月 11 日
Don't be obsessed with cellfun(). A simple for-loop will do
%%
C = {ones(4,5), 2*ones(4,5), 3*ones(4,5); 4*ones(4,5), 5*ones(4,5), 6*ones(4,5)};
B = {7, 8, 9 ; 10, 11, 12};
for k=1:numel(C)
C{k}(:)=B{k};
end
##### 1 件のコメント表示非表示 なし
Susan 2020 年 7 月 13 日
Thank you SO much for your help!

サインインしてコメントする。

### カテゴリ

Find more on Cell Arrays in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by