Writing first and last line of each cell

조회 수: 1 (최근 30일)
Leonardo Barbosa Torres dos Santos
편집: madhan ravi 2019년 4월 7일
Dear, I have a question about write file.
I have a file xxMM. It is a 19x1 cell.
The first cell has 14x6 double. The second cell has 12x6 double. The third 9x6 double. The fourth 6x6 double and etc.
I would like write a file (double) with only the last line of each cell. Only of first cell (14x6) I would like write the first and of last line of cell. All the others I would like to write only the last line
For example, the file is xx.
I would like that xx be a double and that the first line be the first and the last line and the six columns of first file (cell) of xxMM.
The second line of xx be only the last line and the six columns of second file (cell) of xxMM.
The third line of xx be only the last line and the six columns of third file (cell) of xxMM and etc.
I did it:
xx=zeros(length(xxMM),6);
for i=1:length(xxMM)
a=xxMM{i};
xx(i,:)=a(end,:);
But, the form I just write the last line of each cell. But the first cell I would like of first and the last line.
How can I do it ?
Thank you advanced

채택된 답변

dpb
dpb 2019년 4월 7일
편집: dpb 2019년 4월 7일
Just have to handle the special case outside the loop...
xx=zeros(length(xxMM)+1,6);
xx(1,:)=xxMM{1,:}(1,:);
for i=1:length(xxMM)
xx(i+1,:)=xxMM{i}(end,:);
end
or, you could dispense with the explicit for...end loop with cellfun
xx=[xxMM{1}(1,:);cell2mat(cellfun(@(x) x(end,:),xxMM,'uni',0))];
  댓글 수: 3
dpb
dpb 2019년 4월 7일
I did a test here on sample; code worked just fine...post the actual code and error message. Maybe you missed "the curlies" to dereference the cell???
madhan ravi
madhan ravi 2019년 4월 7일
편집: madhan ravi 2019년 4월 7일
xx(1,:)=xxMM{1,:}(1,:);
% ^^^^^----- was missing at the beginning

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by