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

 채택된 답변

Guillaume
Guillaume 2018년 6월 27일

1 개 추천

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

simone sangaletti
simone sangaletti 2018년 6월 27일
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.
simone sangaletti
simone sangaletti 2018년 6월 27일
편집: simone sangaletti 2018년 6월 27일
it is not possible to do it with:
A = {44x3 double,44x3 double,44x3 double,........}
B = {44x1 logical,44x1 logical,44x1 logical,........}
Is it?
Guillaume
Guillaume 2018년 6월 27일
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?
simone sangaletti
simone sangaletti 2018년 6월 27일
yes please, thank you
Guillaume
Guillaume 2018년 6월 27일
편집: Guillaume 2018년 6월 27일
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.
simone sangaletti
simone sangaletti 2018년 6월 27일
perfect, it works!!! thanks
Mack Gardner-Morse
Mack Gardner-Morse 2022년 3월 25일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

제품

릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by