フィルターのクリア

How to reference values in cell array containing matrices

4 ビュー (過去 30 日間)
KAE
KAE 2018 年 1 月 18 日
コメント済み: Star Strider 2018 年 1 月 18 日
I have a cell in which each element is a matrix. I would like to extract a vector containing all the matrices' entries for row 5, column 4. Is this possible, without having to loop through the matrices and extract the value? Here's an example,
% Create the cell array
for iMatrix = 1:10
a{iMatrix} = rand(12, 13); % Each element of this cell array is a matrix
end
iRow = 5; iCol = 4; % Indices to the value to extract from each matrix
% Unsuccessful attempts
a{:}(iRow,iCol)
Expected one output from a curly brace or dot indexing expression, but there were 10 results.
a(:)(iRow,iCol)
Error: ()-indexing must appear last in an index expression.
a(iRow,iCol)
Index exceeds matrix dimensions.

採用された回答

Star Strider
Star Strider 2018 年 1 月 18 日
Use cellfun.
This seems to work:
% Create the cell array
for iMatrix = 1:10
DisplayMatrix = rand(12, 13)
a{iMatrix} = DisplayMatrix; % Each element of this cell array is a matrix
end
iRow = 5; iCol = 4; % Indices to the value to extract from each matrix
Extracted = cellfun(@(x) x(iRow,iCol), a) % Desired Result
  2 件のコメント
KAE
KAE 2018 年 1 月 18 日
Great, that works.
Star Strider
Star Strider 2018 年 1 月 18 日
My pleasure.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by