필터 지우기
필터 지우기

delete the first n elements of each cell

조회 수: 1 (최근 30일)
Chaoyang Jiang
Chaoyang Jiang 2018년 5월 3일
댓글: Chaoyang Jiang 2018년 5월 3일
Given cell a, how to delete the first n elements of each cell? I am now using for loop. However, when the loop is big (5021889 loops), this line can be very slow.
May I know if there is a faster version or vectorized version of this?
Thank you
a=cell(2,1);
a{1,1}=1:10;
a{2,1}=1:10;
b=[3,8];
for i=1:2
a{i,1}(1:b(i))=[];
end

채택된 답변

Stephen23
Stephen23 2018년 5월 3일
I doubt that there is a much faster way, because what you are doing resizes the arrays themselves. cellfun will likely be slower.
  댓글 수: 2
Guillaume
Guillaume 2018년 5월 3일
Stephen is right, it's unlikely you can get faster (short of going the mex way and even then...)
However, when the loop is big (5021889 loops), this line can be very slow
No matter how you do it in matlab, truncating the first n elements of 5021889 array ultimately requires for each array:
  • the reallocation of a new chunk memory. There's a lot of background check that needs to be done before memory is allocated so that's not a fast operation (relatively speaking),
  • the copy of the required portion of original data in that new chunk of memory.
This has to be done 5021889 times, so it's going to be slow no matter what you do.
The only way to speed this up is to change your algorithm completely so that it is not required.
Chaoyang Jiang
Chaoyang Jiang 2018년 5월 3일
Thank you for your answer.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by