Got Questions? Get Answers.
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:
Combining cell array by rows

Subject: Combining cell array by rows

From: Mike Sheppard

Date: 20 Dec, 2010 16:01:08

Message: 1 of 4


Can't quite seem to figure it out. Here's example Input and Output of what I'm looking for:

Input={'ab' 'cd' 'ef';'gh' 'ij' 'kl';'mn' 'op' 'qr';'st' 'uv' 'wx'}
Output={'abcdef';'ghijkl';'mnopqr';'stuvwx'}

The length of the strings in each cell may be variable in length, as well as the dimensions of the Input from one input to another.

Basically just want to combine everything within a same row, so Output{n} is the entire n'th row in one long string.

Thanks!

-MIke

Subject: Combining cell array by rows

From: Mike Sheppard

Date: 20 Dec, 2010 16:12:06

Message: 2 of 4


I think I figured it out, but it looks like it could be something simpler:

Input={'ab' 'cd' 'ef';'gh' 'ij' 'kl';'mn' 'op' 'qr';'st' 'uv' 'wx'}

OuputGiven=arrayfun(@(x)(char(cell2mat(Input(x,:)))),[1:size(Input,1)],'Uniformoutput',false)'

OutputWant={'abcdef';'ghijkl';'mnopqr';'stuvwx'}

Subject: Combining cell array by rows

From: Sean de

Date: 20 Dec, 2010 16:30:29

Message: 3 of 4

"Mike Sheppard" <guinness42@hotmail.com> wrote in message <ienv8m$nnl$1@fred.mathworks.com>...
>
> I think I figured it out, but it looks like it could be something simpler:
>
> Input={'ab' 'cd' 'ef';'gh' 'ij' 'kl';'mn' 'op' 'qr';'st' 'uv' 'wx'}
>
> OuputGiven=arrayfun(@(x)(char(cell2mat(Input(x,:)))),[1:size(Input,1)],'Uniformoutput',false)'
>
> OutputWant={'abcdef';'ghijkl';'mnopqr';'stuvwx'}

Under the assumption that the strings are all the same size:

X = {'ab' 'cd' 'ef';'gh' 'ij' 'kl';'mn' 'op' 'qr';'st' 'uv' 'wx'}; %input is matlab function
Out = reshape(char(X')',size(X,2)*numel(X{1}),size(X,1)).'

Subject: Combining cell array by rows

From: Matt Fig

Date: 20 Dec, 2010 17:44:07

Message: 4 of 4

Also,


Input={'ab' 'cd' 'ef';'gh' 'ij' 'kl';'mn' 'op' 'qr';'st' 'uv' 'wx'};
Output = mat2cell(cell2mat(Input),[1 1 1 1],6)

Tags for 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