Manipolation of cell array and

I have three cell array as per below and I would like to create a for loop that read directly one per time the three array and do the sum of the numeric value (columns 3 and for) for each groups. Is it possible?
Groups{1}
Groups{2}
Groups{3}
ans =
'a' 'b' [1] [2] [3]
'a' 'b' [3] [4] [5]
ans =
'b' 'z' [3] [4] [5]
'b' 'z' [4] [5] [6]
ans =
'r' 't' [5] [6] [7]
'r' 't' [6] [7] [8]

 Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 20 Dez. 2011

1 Stimme

Do you want to sum the 3rd and 4th column all together or separately? Anyway, it should be similar to this.
Out=zeros(size(Groups));
for k=1:numel(Groups)
Out(k)=sum([Groups{k}{:,3}]);
end

4 Kommentare

Maurizio
Maurizio am 20 Dez. 2011
An if I would like to have the average of colums 4 of each group.
Fangjun Jiang
Fangjun Jiang am 20 Dez. 2011
mean([Groups{k}{:,4}]);
Maurizio
Maurizio am 20 Dez. 2011
is it possible to have all the output sum in just one cell instead of k cell struct?
Fangjun Jiang
Fangjun Jiang am 20 Dez. 2011
The variable Output in the above code is not a cell. It is a double array. If you want to put it in a cell, just use {Output}

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Titus Edelhofer
Titus Edelhofer am 20 Dez. 2011

0 Stimmen

Hi,
yes, should be not too difficult, as long as you know where the numeric values are:
values = zeros(2,3);
for i=1:length(Groups)
values = values + cell2mat(Groups{i}(:, 3:5));
end
Titus

1 Kommentar

Maurizio
Maurizio am 20 Dez. 2011
An if I would like to have the average of colums 4 of each group.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-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