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
|
|
|
|
Reply
|
Shal
|
3/19/2010 3:54:04 PM |
|
"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
|
|
|
|
Reply
|
us
|
3/19/2010 4:00:20 PM
|
|
> 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
|
|
|
|
Reply
|
Oleg
|
3/19/2010 5:04:05 PM
|
|
"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
|
|
|
|
Reply
|
Shal
|
3/19/2010 5:20:19 PM
|
|
"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
|
|
|
|
Reply
|
Bruno
|
3/19/2010 6:20:21 PM
|
|
|
4 Replies
585 Views
(page loaded in 0.039 seconds)
|