Replacing a cell array column with another cell array column (conversion to cell from double is not possible)
이전 댓글 표시
Background:
A is a 1x51 cell - containing 51 Mx6 cells.
Snippet:

B is a 1x51 cell - containing 51 Mx1 doubles.
Snippet:

I want to remove columns 4 and 5 from all 51 cells of A and replace them with the column vectors located in B. So in A{1,1}(:,4:5) is replaced with B{1,1} (B being a cell of column vectors), in A{1,2}{:,4:5} is replaced with B{1,2}.
Since B is doubles and A is cells I can't simply do this:
A{1,1}(:,4) = B{1,1};
A{1,1}(:,5) = [];
Conversion to cell from double is not possible.
>>>>>
Error in IMPORT (line 129)
A{1,1}(:,4)=B{1,1};
So my question is: how do I go about replacing those columns and then how do I loop it through the whole of A to replace each cell's columns? (I'm assuming this can be done using something like cellfun?)
Thanks in advance.
채택된 답변
추가 답변 (1개)
Guillaume
2014년 11월 27일
As Image Analyst said, you're trying to replace cells with a matrix, so first you need to convert that matrix into cells:
A = {cell(111,6), cell(50,6), cell(53,6), cell(74,6), cell(120,6)};
B = {rand(111,1), rand(50,1), rand(53,1), rand(74,1), rand(120,6)};
for col = 1:numel(A)
A{col}(:, 4) = num2cell(B{col});
A{col}(:, 5) = [];
end
댓글 수: 4
Stephen
2014년 11월 28일
The A and B construction is just there for generating some demonstration data.
The rest of the code makes no assumption on the size of the cell arrays, other than the matrices in B are column vectors with the same numbers of rows as the respective cell arrays in A.
The code is already fully automated.
Stephen
2014년 11월 28일
Guillaume
2014년 11월 28일
The best way for thanking people is to accept their answer.
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

