How to extract element of all matrices within a cell array using cellfun?
2 ビュー (過去 30 日間)
古いコメントを表示
I need to extract (2,1) i.e. second row and first column element of all matrices within cell array ?
0 件のコメント
採用された回答
Image Analyst
2018 年 12 月 6 日
Are all the matrices in the cells the same size? If so, just use cell2mat() and indexing:
[rows, columns] = size(ca{1})
m = cell2mat(ca)
m21 = m(2:rows:end, 1:columns:end)
5 件のコメント
Image Analyst
2018 年 12 月 7 日
Of course, you could always use a simple for loop:
output = ones(1, numel(ca));
for k = 1 : numel(ca)
thisCellsContents = ca{k}; % Extract this one particular cell
output(k) = thisCellsContents(2, 1);
end
その他の回答 (2 件)
Jos (10584)
2018 年 12 月 6 日
This looks like homework to me. Here is a big hint:
Create a function that extracts this for you and use cellfun to apply it to all cells ...
MyFun = @(M) M(...)
5 件のコメント
James Tursa
2018 年 12 月 6 日
編集済み: James Tursa
2018 年 12 月 6 日
The value of the (2,1) element makes no differrence to indexing ... whether it is finite or inf or NaN. You will not get an "index exceeds" error just because the element is NaN. You get the error because there is no (2,1) element. You need to re-check your matrices. What do you get when you run this:
C = your cell array of matrices
min(cellfun(@numel,C))
min(cellfun(@(c)size(c,1),C))
Ali Nik
2023 年 4 月 16 日
Hi I want to use all nested arrays inside each cell. Thank you for your help His photo is attached.
1 件のコメント
Image Analyst
2023 年 4 月 16 日
Not sure how this is an answer to @NS. And when you said "His photo is attached", we're not sure what photo you're talking about or where it is. Certainly NS's photo is not attached to your Answer.
If you have your own question, start a new question and attach your cell array in a .mat file after you read this:
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!