MATLAB Answers

## Subtracting two cell arrays, yielding a third cell array

Asked by Andrew

### Andrew (view profile)

on 4 Jun 2011
Latest activity Commented on by Rahav

### Rahav (view profile)

on 5 Apr 2015
Accepted Answer by Matt Fig

### Matt Fig (view profile)

Hi,

I have two cell array of vectors; each cell in both cell arrays contains a 1 by 3 vector. For example,

```A={[20 20 20],[30 30 30],[40 40 40];[50 50 50],[60 60 60],[70 70 70]};
B={[1 1 1],[2 2 2],[3 3 3];[4 4 4],[5 5 5],[6 6 6]};
```

How can I subtract B from A? For example, I wish to find:

```C={[19 19 19],[28 28 28],[37 37 37];[46 46 46],[55 55 55],[64 64 64]}
```

Is there a way I can do this without stepping through each cell, converting them to numeric arrays using cell2mat, and then converting them back to cells?

Thank you very much for your time.

Andrew DeYoung

Carnegie Mellon University

## 1 Answer

### Matt Fig (view profile)

Answer by Matt Fig

### Matt Fig (view profile)

on 4 Jun 2011
Accepted answer
```C = cellfun(@minus,A,B,'Un',0)
```

Rahav

### Rahav (view profile)

on 5 Apr 2015
```C = cellfun(@minus,A,B,'UniformOutput',false)
```

is clearer.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

New to MATLAB?