Index and remove values in a cell array

조회 수: 30 (최근 30일)
Richard Rees
Richard Rees 2019년 12월 3일
댓글: Richard Rees 2019년 12월 4일
Good evening,
I have a problem with my code for removing the initial values of certain rows of matrices within cell arrays. The problem is that I do not how how to index locations where the conditional of removal is meet, while using a while loop and remove them afterwards, hence the probelm below deleting within the loop.
Could someone help please?
min_sc = min(cellfun(@length,ixv));
a = ixv{size(ixv,2)}(1,1); % get the first column of the final cell value
% Remove inital rows < end cell values.
for k = 1:size(ixv,2)
for v = 1:min_sc
while ixv{k}(v,1)< a
ixv{k}(v,:) = [];
xc{k}(v) = [];
end
while xc_1{k}(v)<a-1 % New while loop because of new condition
xc_1{k}(v) = [];
end
end
end

답변 (1개)

Hank
Hank 2019년 12월 3일
편집: Hank 2019년 12월 3일
Its hard to see what the purpose of this deletion is. It looks like you have multiple (499) datasets described by ixv (some kind of index) and by xc or xc_1 (the data). You want to delete values at the begining of the dataset where the index starts after the first index value of the last dataset (a=11).
I tried rewriting your code to be a little more followable. I used the find function to delete in one step instead of using a while loop.
I find that deleting values with a while loop can be problematic because the index of the while loop and the size of the array become out of sync as values are removed. I'm not sure if thats whats happening here though.
Tell me if this does what you're looking for. If not, let me know why, maybe i'll understand you better.
a = ixv{end}(1,1) % first value of last dataset
for k = 1:numel(ixv);
across = find(ixv{k}(:,1)<a,1) % find the first place where the index becomes larger than a.
across_1 = find(xc_1{k})<a-1,1) % same for xc_1 condition
% delete values before across
ixv{k}(1:across-1,:) = [];
xc{k}(1:across-1) = [];
xc_1{k}(1:across_1-1) = [];
end
  댓글 수: 3
Hank
Hank 2019년 12월 4일
편집: Hank 2019년 12월 4일
I think I just made a parenthesis error. Try this; it worked for me.
a = ixv{end}(1,1); % first value of last dataset
for k = 1:numel(ixv);
across = find(ixv{k}(:,1)<a,1) % find the first place where the index becomes larger than a.
across_1 = find(xc_1{k}<a-1,1) % same for xc_1 condition
% delete values before across
ixv{k}(1:across-1,:) = [];
xc{k}(1:across-1) = [];
xc_1{k}(1:across_1-1) = [];
end
Richard Rees
Richard Rees 2019년 12월 4일
Hi, it is still not doing what it should be doing. Attached is a screen shot showing the ixv{1,287} and {1,409} as an example. The variable a = 11, so these shouldn't be appearing.

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

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by