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

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Priya on 26 May 2013

I have a cell array as follows:

no. of columns = 3 and no. of rows = 800 First Column - Serial No Second Column - Character Third Column - Number

cell = {1 'N' 1.2 ; 2 'B' 3.2 ; 3 'D' 5.3 ; 1 'N' 1.2 ; 2 'A' 3.2 ; 3 'G' 5.3 ; 1 'E' 1.2 ; 2 'F' 3.2 ; 3 'G' 5.3 ; 1 'N' 1.2 ; 2 'C' 3.2 ; 3 'D' 5.3 ; 1 'H' 1.2 ; 2 'B' 3.2 ; 3 'D' 5.3 ; 1 'N' 1.2 ; 2 'B' 3.2 ; 3 'E' 5.3 ....................}

In second column character 'N' repeats after certain entries (rows).

I want to add the numbers in third coulmn from the row with 'N' in second column till the rows before next 'N' appears in second row.

*No products are associated with this question.*

Answer by Azzi Abdelmalek on 26 May 2013

idx1=find(cellfun(@(x) isequal(x,'N'),cell(:,2))); idx2=[idx1(2:end)-1; size(cell,1)]; for k=1:numel(idx1) h{k}=cellfun(@(x) x+cell{idx1(k),3},cell(idx1(k):idx2(k),3)); end h=cell2mat(h'); cell(:,3)=num2cell(h(:));

## 2 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/77005#comment_151039

What should be the result?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/77005#comment_151069

"cell" is an important Matlab command. Using this term as a name of a variable

shadowsthe built-in function and this causes troubles frequently.