Contents

- 1 Problem 41. Cell joiner
- 2 Problem 152. Create a cell array out of a struct
- 3 Problem 380. Convert a numerical matrix into a cell array of strings
- 4 Problem 967. Split a string into chunks of specified length
- 5 Problem 1899. Convert a Cell Array into an Array
- 6 Problem 1971. Remove element(s) from cell array
- 7 Problem 1036. Cell Counting: How Many Draws?
- 8 Problem 43677. String Array Basics, Part 1: Convert Cell Array to String Array; No Missing Values
- 9 Problem 1755. Fix the last element of a cell array
- 10 Problem 2300. Natural numbers in string form

Learn Matlab and earn a badge and skill. This section contains the *Cody solution* of** Basics on Cell Arrays ****Cody Solution**. Total 10 solved problems.

Visit the Matlab official Problems page of Basics on Cell Arrays.

All solutions were verified and working. It may be possible after a year or more community may modify the question, then the provided solution may not work so do comment below about the same. Our team will suddenly work on it.

JOIN US ON TELEGRAM

Get Notified Faster

## Problem 41. Cell joiner

You are given a cell array of strings and a string delimiter. You need to produce one string which is composed of each string from the cell array separated by the delimiter.

For example, this input

in_cell = {‘Lorem’, ‘ipsum’, ‘dolor’, ‘sit’, ‘amet’, ‘consectetur’};

delim = ‘ ‘;

should produce this output:

out_str = ‘Lorem ipsum dolor sit amet consectetur’;

```
function out_str = cellstr_joiner(in_cell, delim)
out_str = string(join(in_cell,delim));
end
```

## Problem 152. Create a cell array out of a struct

Create a cell array out of a (single) struct with the fieldname in the first column and the value in the second column:

in:

S.foo = ‘hello’;

S.bar = 3.14;

out:

{‘foo’, ‘hello’;

‘bar’, 3.14}

```
function c = struct2namedCell(S)
c = [fieldnames(S), struct2cell(S)]
end
```

## Problem 380. Convert a numerical matrix into a cell array of strings

Given a numerical matrix, output a cell array of string.

For example:

if input = 1:3

output is {‘1′,’2′,’3’}

which is a cell 1×3

```
function output = matrix2cell(input)
output = string(num2cell(input));
end
```

## Problem 967. Split a string into chunks of specified length

Given a string and a vector of integers, break the string into chunks whose lengths are given by the elements of the vector. Extra characters leftover at the end should be dropped. Example:

break_string(‘I seem to be having tremendous difficulty with my lifestyle’,[4 1 5 22])

should return a 4×1 cell array:

ans =

‘I se’ ‘e’ ‘m to ‘ ‘be having tremendous d’

```
function y = break_string(s,b)
for i=1:length(b)
y{i}=s(1:b(i));
s=s(b(i)+1:end);
end
end
```

## Problem 1899. Convert a Cell Array into an Array

Given a square cell array:

x = {’01’, ’56’; ‘234’, ‘789’};

return a single character array:

y = ‘0123456789’

```
function y = your_fcn_name(x)
y = strjoin(x, '');
end
```

## Problem 1971. Remove element(s) from cell array

You can easily remove an element (or a column in any dimension) from a normal matrix, but assigning that value (or range) empty. For example

A = 1:10

A(5) = []

results in

1 2 3 4 6 7 8 9 10

You task is to find the shortest, elegant, way in Matlab to do the same for cell arrays. Regexp, eval, and other workarounds that trick mtree are considered stupid, and will not be appreciated.

```
function y = remove_from_cell_array(x,to_remove)
x(to_remove) = [];
y = x(~cellfun(@isempty, x))
end
```

## Problem 1036. Cell Counting: How Many Draws?

You are given a cell array containing information about a number of soccer games. Each cell contains one of the following:

- ‘H’, meaning the home team won
- ‘A’, meaning the away team won
- ‘D’, meaning a draw, or tie game

So if

games = {‘D’,‘D’,‘A’,‘H’,‘D’,‘H’}

then

draws = 3

```
function draws = how_many_draws(games)
draws = size(games(cellfun(@(x) strcmp('D', x), games)),2)
end
```

## Problem 43677. String Array Basics, Part 1: Convert Cell Array to String Array; No Missing Values

String array and cell array are two types of containers for storing pieces of data. In this problem, you will be given a cell array of character vectors. Your job is to convert the cell array to a string array, which stores the same pieces of text data.

To begin with, let’s assume that there are no missing type values in the input cell array.

Example:

Input:

>> x = {‘I’,‘Love’,‘MATLAB’}

x =

1×3 cell array

‘I’ ‘Love’ ‘MATLAB’

Output:

>> y = strings(size(x));

>> [y{:}] = x{:}

y =

1×3 string array

“I” “Love” “MATLAB”

Note that the example shown above is not the best way to solve this problem. Try other approaches in order to achieve a leading score.

```
function y = cell2str(x)
y = string(x);
end
```

## Problem 1755. Fix the last element of a cell array

Given a cell array of strings

A = {‘MATLAB’,‘HURRAY’,‘SPARKLY’,‘KITTENS’,‘FUN’};

and a particular string value

B = ‘KITTENS’;

ensure that B is the last element of the cell array. If it isn’t, move it to the end of A.

You cannot assume that B appears at all (in which case return A unchanged), but you can assume B does not appear more than once.

So in the example,

C = {‘MATLAB’,‘HURRAY’,‘SPARKLY’,‘FUN’,‘KITTENS’};

```
function C = puzzler(A,B)
C = A;
D = find(strcmp(A,B));
if ~isempty(D)
C(D) = [];
C(end+1) = cellstr(B);
end
end
```

## Problem 2300. Natural numbers in string form

Create a cell array of strings containing the first n natural numbers. Slightly harder than it seems like it should be.

Example

>> y = naturalnumbers(4)

y =

‘1’ ‘2’ ‘3’ ‘4’

```
function y = naturalnumbers(x)
a=[1:1:x]
y=strsplit(num2str(a))
end
```

**Basics on Cell Arrays** 10 solved problems. All solution is correct as they were first submitted in Matlab and then uploaded here for your help. If any solution doesn’t work then do comment.

Also Check the solution of other Matlab Cody Contest Matlab Cody More Contest solutions.

MarkThanks for your blog, nice to read. Do not stop.