How can i delete every n-th row in a cell array

조회 수: 7 (최근 30일)
Manu Mensa
Manu Mensa 2020년 12월 4일
댓글: Manu Mensa 2020년 12월 7일
I have a cell array like C=cell(10,1). I want to delete every n-th row of the cell and have the result in a cell array with.
I have used
rowstodel=1:10
p(rowstodel(1),:)=[]; % delete first row
However, i want the resulting 9x1 results in a cell array. I have tried using a for loop but i keep getting 'matrix is out of range for deletion'
Thanks

채택된 답변

Image Analyst
Image Analyst 2020년 12월 5일
편집: Image Analyst 2020년 12월 5일
Try this:
C = cell(10,1) % Create a cell array (column vector) of 10 individual cells (empty ones).
whos C
% Delete very 3rd row starting at 1 by setting those rows to null.
% I.e. delete cells 1, 4, 7, and 10
% leaving only 6 cells in the cell array.
C(1:3:end) = []
whos C
you'll see:
C =
10×1 cell array
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
Name Size Bytes Class Attributes
C 10x1 80 cell
C =
6×1 cell array
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
Name Size Bytes Class Attributes
C 6x1 48 cell
See the FAQ:
  댓글 수: 3
Image Analyst
Image Analyst 2020년 12월 6일
So simply make a copy of it before you delete the rows:
C = cell(10,1) % Create a cell array (column vector) of 10 individual cells (empty ones).
whos C % Has 10 rows and one column. Each element is a cell, which is empty so far.
% Delete very 3rd row starting at 1 by setting those rows to null.
% I.e. delete cells 1, 4, 7, and 10
% leaving only 6 cells in the cell array.
C2 = C; % Make a copy.
% Delete the rows from the copy.
C2(1:3:end) = []
whos C2
Manu Mensa
Manu Mensa 2020년 12월 7일
Thank you, Image Analyst.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by