Replacing a cell array column with another cell array column (conversion to cell from double is not possible)

26 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 27 Nov. 2014
Let's look at what
A{1,1}(:,4) = B{1,1};
actually is. A{1,1} is taking the upper left cell in the 1 by 51 cell array, in other words the first cell since it's a row vector. Since you used braces, you're extracting the cell contents rather than referring to the cell itself. But the cell contents is yet another cell array! It's a cell array of 111 rows of cells by 6 columns of cells. So when you say A{1,1}(:,4), you're taking all the rows in column 4 of that 6 column cell array. So now you have a column vector of cells that is 111 cells tall. Inside each of those 111 cells is a double array, but the expression itself is a column of cells, not doubles , and therein lies the problem.
Now, let's look at B{1,1}. You're using braces so you're extracting the contents of the upper left cell of the B cell array. The contents of that cell is a 111 by 1 column vector of doubles.
Knowing this, and after reading the FAQ, take another crack at it.
  2 Kommentare
Stephen
Stephen am 27 Nov. 2014
I was actually reluctant to post a question having done a fair amount of digging around prior to doing so, I've previously read through the FAQ. If you look at the data within A and B (show below) you'll understand where my confusion is coming from.
Here is a snippet of column 4 and 5 from A{1,1}
Here is a snippet from B{1,1}
Reiteration of my original question, simply imagine that the data from A{1,1} in column 4 and 5 was replaced with the data in B{1,1}. The frustration is simply not knowing how to write it.
Image Analyst
Image Analyst am 27 Nov. 2014
If Guillaume's solution doesn't do it, then please attach a mat file so we can try some things.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 27 Nov. 2014
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 Kommentare
Stephen
Stephen am 28 Nov. 2014
I was zoned out (too early), this does exactly what I wanted. For some reason my brain was only thinking about processing the conversion and replacement as two seperate things - your method of grouping it all into a loop was something I should have thought about.
Many thanks.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by