Index and remove values in a cell array
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Hank
am 3 Dez. 2019
Bearbeitet: Hank
am 3 Dez. 2019
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 Kommentare
Hank
am 4 Dez. 2019
Bearbeitet: Hank
am 4 Dez. 2019
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
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!