Code Comments
Programming Forum and web based access to our favorite programming groups.I have a cell array of 192 X 64.Now I need to convert each of the individual cells back into matrix(cell2mat), then populate a row of a new matrix with the contents of the cell. Basically I have B = 192 * 64 cell. each is of the form B(1,1)=2x2 double. converting to matrix, B = cell2mat(B(1,1)) Now I need to put this into New(1,1:4)=[B(1,1) B(1,2) B(2,1) B(2,2)]. The question I have is whether this can be done in a easier way since it is'nt always 192*64, I have to go through every single cell, and it is'nt always 2x2.
Post Follow-up to this messageannie wrote: > > > I have a cell array of 192 X 64.Now I need to convert > each of the individual cells back into matrix(cell2mat), then > populate a row of a new matrix with the contents of the cell. > > Basically I have B = 192 * 64 cell. > each is of the form B(1,1)=2x2 double. > converting to matrix, B = cell2mat(B(1,1)) > Now I need to put this into New(1,1:4)=[B(1,1) B(1,2) B(2,1) > B(2,2)]. > > > The question I have is whether this can be done in a easier way > since > it is'nt always 192*64, I have to go through every single cell, and > > it is'nt always 2x2. Help please!
Post Follow-up to this messageannie wrote:
>
>
> annie wrote:
> and
> Help please!
Reshape & Transpose (or .') are your friends ...
% create example data:
for i=1:10,
B{i} = repmat(i,2,2) ;
end
% NB All elements of B have equal elements
NewB = reshape(cell2mat(B),numel(B{1}),[]) .' ;
hth
Jos
-- no X-es in my email
Post Follow-up to this messageannie wrote:
>
>
> annie wrote:
> and
> Help please!
There surely must be a more elegant and efficient way to do it, but
here's a start:
%%%%%%%%%%%%
newB = reshape(B', 1, []);
for i = 1:length(newB)
New(i, :) = reshape(newB{i}', 1, []);
end
%%%%%%%%%%%%
This is if you want to go across the rows first and down the columns
of B. If you want to go down the columns first, then
newB = {B{:}};
Hope this works.
jiro
Post Follow-up to this messageJiro Doke wrote:
>
>
> annie wrote:
then
cell.
B(2,1)
> way
> cell,
>
> There surely must be a more elegant and efficient way to do it, but
> here's a start:
>
> %%%%%%%%%%%%
> newB = reshape(B', 1, []);
>
> for i = 1:length(newB)
> New(i, :) = reshape(newB{i}', 1, []);
> end
> %%%%%%%%%%%%
>
> This is if you want to go across the rows first and down the
> columns
> of B. If you want to go down the columns first, then
>
> newB = {B{:}};
>
> Hope this works.
>
> jiro
Jos,
Very clever. Let me add another thing to your code. If I understand
the OP, she wants to display rows first:
newB = reshape(B', 1, []);
New = reshape(cell2mat(newB')',numel(B{1}),[])
';
Post Follow-up to this messageJiro Doke wrote:
>
>
> Jiro Doke wrote:
matrix(cell2mat),
> then
> cell.
> B(2,1)
> easier
single
> but
>
> Jos,
> Very clever. Let me add another thing to your code. If I
> understand
> the OP, she wants to display rows first:
>
> newB = reshape(B', 1, []);
> New = reshape(cell2mat(newB')',numel(B{1}),[])
';
Jiro,Jos:
Thank you! You saved me a lot of time and more importantly I learnt
something.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.