Accessing cell array data every 2 columns and concatenating them

1 vue (au cours des 30 derniers jours)
ErikaZ
ErikaZ le 27 Juin 2018
Réponse apportée : Rik le 27 Juin 2018
I have cell array A =
{1×137 cell} {1×134 cell} {1×410 cell} {1×411 cell} {1×439 cell} {1×455 cell}
{1×137 cell} {1×134 cell} {1×410 cell} {1×411 cell} {1×439 cell} {1×455 cell}
{1×137 cell} {1×134 cell} {1×410 cell} {1×411 cell} {1×439 cell} {1×455 cell}
I need to concatenate columns 1 & 2 and 3 & 4 and 5 & 6, so cell array A is 3x3
{1×271 cell} {1×821 cell} {1×894 cell}
{1×271 cell} {1×821 cell} {1×894 cell}
{1×271 cell} {1×821 cell} {1×894 cell}
The column size within A is not always the same size.
Thank you,

Réponse acceptée

Rik
Rik le 27 Juin 2018
Easy with cellfun:
%generate random data:
A=cell(3,6);x=[137 134 410 411 439 455];
for r=1:size(A,1)
for c=1:size(A,2)
A{r,c}=num2cell(randi(10,1,x(c)));
end
end
%the actual line of code:
A=cellfun(@(x1,x2) [x1,x2],A(:,1:2:end),A(:,2:2:end),'UniformOutput',0);

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by