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?
Guillaume
Guillaume 2018 年 6 月 27 日
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 ExchangeOperators 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