How to remove cell array satisfying specific conditions
Afficher commentaires plus anciens
Hi all,
I have a cell array with n vectors of different lengths m. I want to remove the cells, whose vectors satisfy a specific condition for which all the elements of the individual vectors need to be scanned. Something like that:
for c = 1 : length(cutForce)
for d = round(length(cutTime{c})/4) : round(3*length(cutTime{c})/4)% searching for data between the 1st and 3rd fourth of the vector
if min(cutForce{c}(d)) < 300
cutForce(c) = [];
end
end
end
Unfortunately the code doesn't work. I am new to programming. Help me please.
Réponse acceptée
Plus de réponses (1)
the cyclist
le 25 Sep 2015
Modifié(e) : the cyclist
le 25 Sep 2015
I have not looked at the details of your algorithm. One likely problem is that as you remove elements of the cell array inside your for loop, the index c begins to point to the wrong element. For example, if you happen to remove the first element, then the next time through the loop, c = 2 refers to the 2nd element of the shortened cell array, which is actually the 3rd element of the original vector.
There are a couple solutions. The easier one to understand for a beginner is to, in your for loop, only identify and store the elements you want to remove (say, in variable indicesToRemove), but don't remove them yet. Then, as a second step, do something like
cutForce(indicesToRemove) = [];
The more sophisticated way to do this is to use the cellfun command, which would allow you to test the condition on all cell array elements at once.
1 commentaire
JH
le 26 Sep 2015
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!