I have:
- {A} which is a 100x1 cell with each cell element being a 44x1 matrix.
- {B} which is a 100x1 logical cell with each cell element being a 44x1 logical matrix.
I would like to know if there is a way to apply {B} to {A}, cell element by cell element, in order to filter the matrices contained in cell {A}.
Thanks

 Accepted Answer

result = cellfun(@(a, b) a(b), A, B, 'UniformOutput', false)
will produce a 100x1 cell array of vectors (since filtering a matrix with a logical array always result in vectors).

8 Comments

thank you, it works great!!!
Although in one case {A} is a 100x1 cell with each cell element being a 44x3 matrix and I'd like to perform the same operation obtaining a 100x1 cell with each element being a matrix with 3 coloumns.
How can I do it?
The code above makes no assumption about the size of the arrays in each cell. As long as the corresponding logical array is the same size, the code will work. You could have
A = {44x1 double, 44x3 double, 10x10x3 double, ...}
As long as
B = {44x1 logical, 44x3 logical, 10x10x3 logical, ...}
it will work.
it is not possible to do it with:
A = {44x3 double,44x3 double,44x3 double,........}
B = {44x1 logical,44x1 logical,44x1 logical,........}
Is it?
It probably can be done depending on how you define the filtering of a 44x3 matrix by a 44x1 logical. Should all 3 rows be filtered with the same 44x1 array?
yes please, thank you
assumption: each cell in B contains a logical row vector with the same number of columns as the corresponding array in A
result = cellfun(@(a, b) a(repmat(b, size(a, 1), 1), A, B, 'UniformOutput', false)
The above simply replicates the logical row vector to match the number of rows in b.
perfect, it works!!! thanks
However, it is not any faster than just doing the for loop. result = cell(100,1); for k = 1:100; result{k} = A{k}(B{k}); end

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!