Create a vector with the non-zero indices of a cell array

3 次查看(过去 30 天)
I have a cell array. I need to create a column vector that returns the non-empty row indices.
I have tried this way but there is something to improve.
idx = find(~cellfun(@isempty,matrix),1);
% idx = [2;4]; % this is the result I need to get

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-7-26
%Random data
y = {[],[];[] repmat({rand},3,3); [] []; repmat({rand},4,3) []}
y = 4×2 cell array
{0×0 double} {0×0 double} {0×0 double} {3×3 cell } {0×0 double} {0×0 double} {4×3 cell } {0×0 double}
%Find if all cells in a row are empty or not and then negating
z1 = ~all(cellfun('isempty', y),2)
z1 = 4×1 logical array
0 1 0 1
idx1 = find(z1)
idx1 = 2×1
2 4
Another approach using any() instead of all() -
%Find if any cells in a row are not empty
z2 = any(~cellfun('isempty', y),2)
z2 = 4×1 logical array
0 1 0 1
idx2 = find(z2)
idx2 = 2×1
2 4

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by