MATLAB Answers


Manipulation of multiple cell arrays

Asked by Nikolaos Zafirakis on 23 Jul 2019
Latest activity Commented on by Nikolaos Zafirakis on 23 Jul 2019
I have two questions one to improve my code and the other to perform an operation. I have a series of cell arrays and they are divided into groups as seen in the figure.
1) I currently need to filter out some measurements I found the below two pieces of code effective I was wondering how I could add and & into the expression to perform what I need in one go. Task is to filter out any measurement of all the column six in all cell arrays which are larger than 1.1 and smaller than 0.9.
O = cellfun(@(n) n(n(:,6)>=0.9,:),Bb,'UniformOutput',false);
L = cellfun(@(n) n(n(:,6)<=1.1,:),O,'UniformOutput',false);
2) After the previous two operations, I manage to filter out any lines having the criteria I do not need. Next I want to manipulate column five within each cell array such that it becomes a kind of counter for each cell thus I desire column five to contain 0,1,2,3,4,5,6,7...... for each cell and repeat at the next one starting at 0 again. I highlighted column five of the first cell array below “this figure is the result of operations in step one".


Sign in to comment.

1 Answer

Answer by Bob Nbob
on 23 Jul 2019
 Accepted Answer

1) You don't want an & for this type of logic, because you want to remove anything that is less than 0.9, OR greater than 1.1. If you are looking for things which are less than 0.9 AND greater than 1.1, then you are looking for an impossible condition, and will not filter any numbers out because both conditions cannot be true at the same time. Use | or || for 'or,' and & or && for 'and.'
O = cellfun(@(n) n(n(:,6)>=0.9 | n(:,6)<=1.1,:),Bb,'UniformOutput',false);
2) I'm a bit confused what you're looking for here. I see that you're looking to sort the elements in ascending order, but I don't entirely understand how the repetition comes into play. Are you referring to repeating the sort for each cell? That type of operation should look something like the following:
O = cellfun(@(n) sortrows(n,5),O,'UniformOutput',0);


Thanks for your answer.
1) Thanks works!
2) So I don't exactly want to sort them, because they are already in a sorted order. What I want is to overwrite the value inside to get instead of 1,9,10,11,12 as seen in the figure get 1,2,3,4,5. Hope that clears it up. You could say I would like to overwrite column 5 within each cell with the length of the cell itself.
Hmm, because you're looking to replace specific parts of the cells, I think you might have to resort to a for loop. I'm not entirely sure how to do a replacement like that with something like cellfun.
for i = 1:size(O,1)
O{i}(:,5) = [1:size(O{i},1)];
Thanks a lot that will do however if anyone reads this post and knows how to solve this without a loop please let me know!

Sign in to comment.