replicating the elements of a cell matrix

7 Ansichten (letzte 30 Tage)
Sabbas
Sabbas am 26 Jul. 2012
Beantwortet: Sindar am 6 Sep. 2018
Dear all,
here is a cell matrix
k={
12 'kol'
23 'Sew'
45 'awe'
44 'wow'
}
I would like to replicate each element twice so as to have
k={
12 'kol'
12 'kol'
23 'Sew'
23 'Sew'
45 'awe'
45 'awe'
44 'wow'
44 'wow'
}
I was thinking something like
cr=cellfun(@(x) repmat(x,2,1),k,'uni',false);
but I do not know if I am correct. Is there any better code?
Would there any difference if I had
k={
'12' 'kol'
'23' 'Sew'
'45' 'awe'
'44' 'wow'
}
thanks

Akzeptierte Antwort

the cyclist
the cyclist am 26 Jul. 2012
Here's one way:
nk = size(k,1);
idx = [1:nk;1:nk];
k(idx(:),:)
  2 Kommentare
Sabbas
Sabbas am 26 Jul. 2012
Bearbeitet: Sabbas am 26 Jul. 2012
if I wanted to replicate each element of the above matrix 1000 times instead of 2 how could I modify your code?
thanks
the cyclist
the cyclist am 26 Jul. 2012
Use
idx = repmat(1:nk,[1000 1]);
in place of the second line.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sindar
Sindar am 6 Sep. 2018
repelem(k,2,1)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by