For Programmers: Free Programming Magazines  


Home > Archive > Matlab > April 2005 > Matrix to array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Matrix to array
annie

2005-04-28, 4:06 am

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.
annie

2005-04-28, 4:06 am

annie 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!
Jos

2005-04-28, 4:06 am

annie 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
Jiro Doke

2005-04-28, 4:06 am

annie 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
Jiro Doke

2005-04-28, 4:06 am

Jiro Doke wrote:
>
>
> annie wrote:
then[color=darkred]
cell.[color=darkred]
B(2,1)[color=darkred]
> 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}),[])
';
annie

2005-04-28, 9:00 am

Jiro Doke wrote:
>
>
> Jiro Doke wrote:
matrix(cell2mat),[color=darkred]
> then
> cell.
> B(2,1)
> easier
single[color=darkred]
> 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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com