필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Can you help with logic behind this problem? Iterations

조회 수: 1 (최근 30일)
Priya
Priya 2013년 5월 26일
마감: MATLAB Answer Bot 2021년 8월 20일
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.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 26일
What should be the result?
Jan
Jan 2013년 5월 26일
"cell" is an important Matlab command. Using this term as a name of a variable shadows the built-in function and this causes troubles frequently.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 26일
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(:));
  댓글 수: 1
Jan
Jan 2013년 5월 26일
What about this for getting idx1:
idx1 = find(strcmp(c(:, 2), 'N'));

이 질문은 마감되었습니다.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by