COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### repmat elements of cell array

• Email
• Follow

```Suppose I have a numeric cell, i.e.

b= [1 2 3 4 5 6];
B= num2cell(b);

B=
[1]    [2]    [3]    [4]    [5]    [6]

How do I replicate the elements within cell
I want something like this:

C=
[2x2 double]  [2x2 double]  [2x2 double] [2x2 double] [2x2 double] [2x2 double]

where on expansion, the values within cell B is

C=
[1 1       [2 2
1 1]       2 2] ........

If I use repmat, C= repmat(B,2,2), it replicates the whole cell and not the elements within cell
--
Thanks
Shal
```
 0

See related articles to this posting

```"Shal" <shalini_priti@yahoo.co.in> wrote in message <ho06ms\$76m\$1@fred.mathworks.com>...
> Suppose I have a numeric cell, i.e.
>
> b= [1 2 3 4 5 6];
> B= num2cell(b);
>
> B=
>     [1]    [2]    [3]    [4]    [5]    [6]
>
> How do I replicate the elements within cell
> I want something like this:
>
> C=
>    [2x2 double]  [2x2 double]  [2x2 double] [2x2 double] [2x2 double] [2x2 double]
>
> where on expansion, the values within cell B is
>
> C=
>      [1 1       [2 2
>       1 1]       2 2] ........
>
> If I use repmat, C= repmat(B,2,2), it replicates the whole cell and not the elements within cell
> --
> Thanks
> Shal

one of the solutions

c=num2cell(1:4);
cr=cellfun(@(x) repmat(x,2,2),c,'uni',false);
cr{1}
%{
%    ans =
1     1
1     1
%}

us
```
 0

```>      c=num2cell(1:4);
>      cr=cellfun(@(x) repmat(x,2,2),c,'uni',false);

num2cell can be avoided:
cr = arrayfun(@(x) repmat(x,2,2),b,'un',0);

Slightly faster:
cr2 = arrayfun(@(x) x.*ones(2),b,'un',0);

Oleg
```
 0

```"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ho0aq5\$g84\$1@fred.mathworks.com>...
> >      c=num2cell(1:4);
> >      cr=cellfun(@(x) repmat(x,2,2),c,'uni',false);
>
> num2cell can be avoided:
> cr = arrayfun(@(x) repmat(x,2,2),b,'un',0);
>
> Slightly faster:
> cr2 = arrayfun(@(x) x.*ones(2),b,'un',0);
>
> Oleg

--
Thanks, it did help.
```
 0

```"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ho0aq5\$g84\$1@fred.mathworks.com>...

>
> Slightly faster:
> cr2 = arrayfun(@(x) x.*ones(2),b,'un',0);
>
> Oleg

Much faster

b = 1:1e5;

tic
c = arrayfun(@(x) x.*ones(2),b,'un',0);
toc % 0.742264 seconds

tic
c = cell(size(b));
z = zeros(2);
for i=1:length(b)
c{i} = z+b(i);
end
toc % 0.101876 seconds.

% 2010A, Vista-32, 2-year-old laptop

Bruno
```
 0