# How to perform multiplication in cells?

12 Ansichten (letzte 30 Tage)
Abirami am 28 Jan. 2015
Kommentiert: Stephen23 am 1 Feb. 2015
Hello,
I have a cell array 'y2a' of size 1x128 with each cell containing another cell array of size 1x17(for eg: y2a{1,1} is a 1x17 cell array). I have to multiply the data in each sub-cell (ie; y2a{1,1} or y2a{1,2}...etc)using the following formula
Example:
for cells 1-7
S=(celldata)*(2^(7-i))
for cells 8-16
S=(celldata)*(2^(7-i))
where 'i' is the position of the cell.Since there are only 17 subcells and we use only 16 of them the value of i varies between (1,16).I have included an image of the cell array and the subcell array.
From the image we can clearly see that each cell in y2a have a 1x17 cell in itself. I want to perform the above function for all the subcells present in each y2a(i,i). This I need to do for all the 128 cells present. Is there any inbuilt function in matlab to perform this action? If not how do I do it. Please help. Thanks in advance.
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Guillaume am 28 Jan. 2015
If you're not doing any calculation involving complex numbers, it doesn't matter. Particularly in a function.
Stephen23 am 1 Feb. 2015
"it doesn't matter": in the sense that the code will work, correct. However it is a bad habit that will be difficult to shake later when it really does matter.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Stephen23 am 28 Jan. 2015
Bearbeitet: Stephen23 am 28 Jan. 2015
First option: how about merging the numeric arrays together? Doing this would make these kind of calculations much easier! Read about vectorization to know why.
So assuming that for whatever reason your data absolutely cannot be converted to simple numeric arrays, then why not use arrayfun and cellfun to get the job done. This is a working example of how you could go about doing this:
>> A = {{[0,1],[1,1],[0,0]},{[1,1],[1,0],[0,0]},{[1,1],[0,1],[0,1]}}; %fake data
>> fun = @(C) arrayfun(@(c,n)c{1}*2^(7-n), C(1:2),1:2, 'UniformOutput',false);
>> out = cellfun(fun, A, 'UniformOutput',false);
I simplified your arrays down so that A has three cells, and each cell contains one sub-cellarray which contains three numeric vectors of size 1x2. The required function is defined by c{1}*2^(7-n), and it is preformed only on the contents of the first two cells of each of the sub-cellarray due to the indexing C(1:2),1:2.
But the best solution is to convert your data to simple numeric arrays.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Guillaume am 28 Jan. 2015
Why are you using cell arrays if the matrices they're holding are all the same size? You would be better off with a 128x17 matrix.
The formula you've posted is the same for cells 1-7 and 8-16. And what happens to element 17? Does it just get dropped?
newcell = cellfun(@(subcell) cell2mat(subcell) .* 2.^(7-1:numel(subcell)), y2a, 'UniformOutput', false); %or a variation thereof, once you've clarified what you want.
But honestly, I would just convert the whole lot into a matrix:
m = cell2mat(cellfun(@(subcell) cell2mat(subcell), y2a', 'UniformOutput', false);
newm = bsxfun(@times, m, 2.^(7-size(m, 2)));
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Cell Arrays 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