Index and remove values in a cell array

17 次查看(过去 30 天)
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 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by