Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to convert cell matrix to normal matrix?

Subject: how to convert cell matrix to normal matrix?

From: Nasser M. Abbasi

Date: 17 Jul, 2012 11:17:04

Message: 1 of 17


I am bad with cells. Given

---------------------
clear all;

A = [ 1 2 ;
       3 4 ];
B = [ 1 2 ;
       2 1 ];

R=arrayfun(@(x,y) repmat(x,1,y),A,B,'UniformOutput',false)
-------------------

Now R is

R =
     [ 1] [1x2 double]
     [1x2 double] [ 4]

How to convert this to normal matrix? so that the
result is

R=

      1 2 2
      3 3 4

Tried:
  cell2mat(R) does not work

and

f=@(x) cell2mat(x);
cellfun(f,R, 'UniformOutput' ,false)

Also does not work.

thanks,
--Nasser

Subject: how to convert cell matrix to normal matrix?

From: Greg Heath

Date: 18 Jul, 2012 19:59:22

Message: 2 of 17

"Nasser M. Abbasi" <nma@12000.org> wrote in message <ju3hje$838$1@speranza.aioe.org>...
>
> I am bad with cells. Given
>
> ---------------------
> clear all;
>
> A = [ 1 2 ;
> 3 4 ];
> B = [ 1 2 ;
> 2 1 ];
>
> R=arrayfun(@(x,y) repmat(x,1,y),A,B,'UniformOutput',false)
> -------------------
>
> Now R is
>
> R =
> [ 1] [1x2 double]
> [1x2 double] [ 4]
>
> How to convert this to normal matrix? so that the
> result is
>
> R=
>
> 1 2 2
> 3 3 4
>
> Tried:
> cell2mat(R) does not work
>
> and
>
> f=@(x) cell2mat(x);
> cellfun(f,R, 'UniformOutput' ,false)
>
> Also does not work.

This doesn't work either but may give you a clue:

R{:,:}

Greg

Subject: how to convert cell matrix to normal matrix?

From: Matt J

Date: 18 Jul, 2012 20:24:17

Message: 3 of 17

"Nasser M. Abbasi" <nma@12000.org> wrote in message <ju3hje$838$1@speranza.aioe.org>...
>
> I am bad with cells. Given
>
> ---------------------
> clear all;
>
> A = [ 1 2 ;
> 3 4 ];
> B = [ 1 2 ;
> 2 1 ];
>
> R=arrayfun(@(x,y) repmat(x,1,y),A,B,'UniformOutput',false)
> -------------------
>
> Now R is
>
> R =
> [ 1] [1x2 double]
> [1x2 double] [ 4]
>
> How to convert this to normal matrix? so that the
> result is
>
> R=
>
> 1 2 2
> 3 3 4




 n=size(R,1);
 c=cell(n,1);
 for ii=1:n, c{ii}=[R{ii,:}]; end;
 R=vertcat(c{:})

R =

     1 2 2
     3 3 4

Subject: how to convert cell matrix to normal matrix?

From: Bruno Luong

Date: 18 Jul, 2012 20:39:14

Message: 4 of 17

R = {[1] [2 2];
     [3 3] [4]};

clear S
for i=size(R,1):-1:1
    S(i,:) = [R{i,:}];
end

% Bruno

Subject: how to convert cell matrix to normal matrix?

From: Nasser M. Abbasi

Date: 18 Jul, 2012 21:42:17

Message: 5 of 17

On 7/18/2012 3:39 PM, Bruno Luong wrote:
> R = {[1] [2 2];
> [3 3] [4]};
>
> clear S
> for i=size(R,1):-1:1
> S(i,:) = [R{i,:}];
> end
>
> % Bruno
>


Thanks Bruno, Matt J, and Greg;

I learned something. Here is my attempt now, thanks
to getting insight from your answers:

-------------
  reshape([R{:,:}],2,3)
--------------

ans =

      1 3 2
      3 2 4

The thing I was missing was the use of the outer '[ ]'
to force a row of cells to become a normal row.

--Nasser

Subject: how to convert cell matrix to normal matrix?

From: Nasser M. Abbasi

Date: 18 Jul, 2012 21:46:50

Message: 6 of 17

On 7/18/2012 4:42 PM, Nasser M. Abbasi wrote:
> On 7/18/2012 3:39 PM, Bruno Luong wrote:
>> R = {[1] [2 2];
>> [3 3] [4]};
>>
>> clear S
>> for i=size(R,1):-1:1
>> S(i,:) = [R{i,:}];
>> end
>>
>> % Bruno
>>
>
>
> Thanks Bruno, Matt J, and Greg;
>
> I learned something. Here is my attempt now, thanks
> to getting insight from your answers:
>
> -------------
> reshape([R{:,:}],2,3)
> --------------
>
> ans =
>
> 1 3 2
> 3 2 4
>
> The thing I was missing was the use of the outer '[ ]'
> to force a row of cells to become a normal row.
>

opps, my answer is wrong. never mind. need to go
have my coffee before I answer. So, I guess there is no
way around using a loop to solve this. I was hoping
for a solution without explicit loop. At least seeing
how to use [] helped.

thanks again

--Nasser

Subject: how to convert cell matrix to normal matrix?

From: Bruno Luong

Date: 18 Jul, 2012 21:54:13

Message: 7 of 17

"Nasser M. Abbasi" <nma@12000.org> wrote in message
> I learned something. Here is my attempt now, thanks
> to getting insight from your answers:
>
> -------------
> reshape([R{:,:}],2,3)
> --------------
>
> ans =
>
> 1 3 2
> 3 2 4
>

This command does not give the expected answer. You need to transpose twice since MATLAB array is column wise ranged.

>> Rt=R';
>> reshape([Rt{:}],3,2)'

ans =

     1 2 2
     3 3 4

Also R{:,:}, R{:} give the same comma list.

% Bruno

Subject: how to convert cell matrix to normal matrix?

From: Nasser M. Abbasi

Date: 18 Jul, 2012 22:11:38

Message: 8 of 17

On 7/18/2012 4:54 PM, Bruno Luong wrote:

> This command does not give the expected answer.
>You need to transpose twice since MATLAB array is column wise ranged.
>
>>> Rt=R';
>>> reshape([Rt{:}],3,2)'
>

Yes, thanks. I saw my answer was wrong after posting. The above
works well now.

<SIDE NOTE>
One very annoying thing in Matlab is that one can't do something like

A'(:)

i.e one must break this into 2 lines

B=A';
B(:);

which is very silly if you ask me. It forces one to introduce
an extra variable that might be need again.

For all the power Matlab syntax has, I find this limitation very
strange. Octave has no problem with tail-end operators:

octave:12> A=[1 2;3 4];
octave:13> A'(:)
ans =

    1
    2
    3
    4

Matlab:
--------------
A=[1 2;3 4];
A'(:)
  A'(:)
   |
Error: Unbalanced or unexpected parenthesis or bracket.
------------------

In Octave I think one can also do foo()(1,2) where foo() can
be a function that return a matrix.

Ok, I digress now.

<END SIDE NOTE>

thanks again for the help.

--Nasser

Subject: how to convert cell matrix to normal matrix?

From: Nasser M. Abbasi

Date: 18 Jul, 2012 22:13:15

Message: 9 of 17

On 7/18/2012 5:11 PM, Nasser M. Abbasi wrote:

>
> which is very silly if you ask me. It forces one to introduce
> an extra variable that might be need again.
>

opps,

might be need again ---> might NOT be needed again

--Nasser

Subject: how to convert cell matrix to normal matrix?

From: dpb

Date: 18 Jul, 2012 23:33:03

Message: 10 of 17

On 7/18/2012 5:11 PM, Nasser M. Abbasi wrote:
...

> One very annoying thing in Matlab is that one can't do something like
>
> A'(:)
>
> i.e one must break this into 2 lines
>
> B=A';
> B(:);
...

Well, you really don't have to...

reshape(permute(A,[2,1]),numel(A),1) % :)

Hmmmm...the introduction of multi-level indexing isn't smart enough to
allow the "obvious" syntax, though, huh? One might have hoped that
would have been a fallout of it...

--

Subject: how to convert cell matrix to normal matrix?

From: dpb

Date: 18 Jul, 2012 23:53:28

Message: 11 of 17

On 7/18/2012 6:33 PM, dpb wrote:
> On 7/18/2012 5:11 PM, Nasser M. Abbasi wrote:
> ....
>
>> One very annoying thing in Matlab is that one can't do something like
>>
>> A'(:)
>>
...

> reshape(permute(A,[2,1]),numel(A),1) % :)
  ...

The above was deliberately obuscated--meant to note it reduces to

reshape(A',numel(A),1)

of course, which isn't as neat as the "post-colon" syntax, but
eliminates the intermediate variable w/o _too_ much pain...

--

Subject: how to convert cell matrix to normal matrix?

From: Bruno Luong

Date: 19 Jul, 2012 05:20:40

Message: 12 of 17

dpb <none@non.net> wrote in message <ju7i9q$mst$1@speranza.aioe.org>...

>
> reshape(A',numel(A),1)
>

Or perhaps: reshape(A', [], 1)

In reality we want to do this [A'{:}]

Now that's prohibited, and I can't find a way to achieve that in one line shorter than

[A{reshape(1:numel(A),size(A))'}]

 % Bruno

Subject: how to convert cell matrix to normal matrix?

From: Nasser M. Abbasi

Date: 19 Jul, 2012 06:13:02

Message: 13 of 17

On 7/19/2012 12:20 AM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <ju7i9q$mst$1@speranza.aioe.org>...
>
>>
>> reshape(A',numel(A),1)
>>
>
> Or perhaps: reshape(A', [], 1)
>
> In reality we want to do this [A'{:}]
>
> Now that's prohibited, and I can't find a way to achieve that in one line shorter than
>
> [A{reshape(1:numel(A),size(A))'}]
>
> % Bruno
>

I think the language should not require users to come
up with tricks to do what should be natural and logical.

Matlab is powerful language, yet seems to be really clumsy
in number of places. Mathworks should spend more time
on improving the Matlab language itself (and the GUI/Guide also)
and not just on making more and more toolboxes.

--Nasser

Subject: how to convert cell matrix to normal matrix?

From: Bruno Luong

Date: 19 Jul, 2012 06:45:29

Message: 14 of 17

"Nasser M. Abbasi" <nma@12000.org> wrote in message <ju88he$vi5$1@speranza.aioe.org>...
> Mathworks should spend more time
> on improving the Matlab language itself

It's too late. Because they can't ensure backward compatibility at the same time.

For example, I can't use optional output/input syntax "~" in my code, due to backward compatibility.

Bruno

Subject: how to convert cell matrix to normal matrix?

From: Bjorn Gustavsson

Date: 19 Jul, 2012 07:02:23

Message: 15 of 17

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ju8ae9$2qd$1@newscl01ah.mathworks.com>...
> "Nasser M. Abbasi" <nma@12000.org> wrote in message <ju88he$vi5$1@speranza.aioe.org>...
> > Mathworks should spend more time
> > on improving the Matlab language itself
>
> It's too late. Because they can't ensure backward compatibility at the same time.
>
> For example, I can't use optional output/input syntax "~" in my code, due to backward compatibility.
>
> Bruno
>
But the A'(:) construct could not break backward compatibility, right? There are no current code with that in it today - since that's an error. To want to break-n-reshape an array in that manner is approximately just as common as A(:)', I'd guess.

Björn

Subject: how to convert cell matrix to normal matrix?

From: Bruno Luong

Date: 19 Jul, 2012 07:10:24

Message: 16 of 17

"Bjorn Gustavsson" <bjonr@irf.se> wrote in message
> >
> But the A'(:) construct could not break backward compatibility, right?

You are right, I want to say forward compatibility.

Bruno

Subject: how to convert cell matrix to normal matrix?

From: dpb

Date: 19 Jul, 2012 13:08:24

Message: 17 of 17

On 7/19/2012 2:10 AM, Bruno Luong wrote:
> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message
>> >
>> But the A'(:) construct could not break backward compatibility, right?
>
> You are right, I want to say forward compatibility.

If that were an issue, TMW could never change anything... :)

Compatibility is an issue TMW says they take seriously (and I'm sure
they do have serious discussions) but not to the extent of some
Standard-ized languages where the process of getting a change adopted is
so formalized and open to the entire community during the process. As a
proprietary product, only TMW determines when and how compatibility
breaks or doesn't and has made numerous changes of greater or lesser
degree at a breathtaking pace compared to Standard languages.

Some things do such as this one do seem worthwhile; probably the best
plan is to continue to submit it to TMW as an official enhancement
request as well as the multiple assignment one.

Working up examples of usage that are realistic as support helps I'm
sure. "Everybody else does it" is the teen or younger argument kids try
on their parents and probably has about as much chance of success... :)

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us