필터 지우기
필터 지우기

Deleting elements in cell array constrained by logic

조회 수: 5 (최근 30일)
alicia che
alicia che 2016년 3월 10일
댓글: alicia che 2016년 3월 24일
I have a cell array for example: A = [{4,2,5},{3,5,7},{2,3,8,},{4,5,6}]. The elements in the cell array are time points in a time series from 0 to 10. I have a binary vector such as B = (0,0,0,1,1,0,0,0,0,0,0), which contains 10 elements (same as the size of the time series). I want to then remove any element in A that corresponding to a "1" in B, in other words remove all the 4s and 5s, to get a new cell array: A_new = [{2},{3,7},{2,3,8,},{6}]. Can anyone help? Thanks!
  댓글 수: 1
alicia che
alicia che 2016년 3월 24일
I have an additional problem related to this- I have another cell array C that matches A: A represents onset time points and C are offset time points of the events. When I deleted entries in A with the constrain B, how can I delete those corresponding entries in C? I tried to use A to constrain C, but after the first round of deleting from A, the size changed and the entries in A and C no longer correspond to each other, which is a problem for me...Thanks for helping!!

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2016년 3월 10일
편집: Fangjun Jiang 2016년 3월 10일
B = [0,0,0,1,1,0,0,0,0,0,0];
A = {[4,2,5],[3,5,7],[2,3,8],[4,5,6]};
NewA=cellfun(@(x) x(~ismember(x,find(B))),A,'uni',false)
% a more readable version would be
TP=find(B);
NewA2=A;
for k=1:numel(A)
Index=ismember(A{k},TP);
NewA2{k}(Index)=[];
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by