How can I find the nonzero values in an cells array?.

7 ビュー (過去 30 日間)
Yro
Yro 2021 年 1 月 11 日
コメント済み: Yro 2021 年 1 月 12 日
The following code generates an cell array Index [1x29], where each cell is an array [29x6]:
for i = 1 : size(P1_cell,1)
for j = 1 : size(P1_cell,2)
[Lia,Lib] = ismember(P1_cell{i,j},PATTERNS_FOR_ERANOS_cell{1},'rows');
Index1(i,j) = Lib % 29x6
end
Index{i} = Index1; % 1x29
end
How can I find the nonzero values in an cells array?, and generate an array with the values from that search. I tried the following loop, but it doesn't work, it creates conflict with the previous one:
for i = 1 : length(Index)
for j = 1 : length(Index)
Non_ceros = length(find(Index{:,i}(j,:))); %% I just need the length of the find function output
end
end
I need help, Thanks in advance.
  4 件のコメント
David Hill
David Hill 2021 年 1 月 11 日
Look at nnz() function
Yro
Yro 2021 年 1 月 11 日
Thanks, but I need the number of non-zero values in each row, nnz function return the number of the nonzeros values in the full array.

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

採用された回答

David Hill
David Hill 2021 年 1 月 11 日
Non_ceros=zeros(length(Index));
for i = 1 : length(Index)
for j = 1 : length(Index)
Non_ceros(i,j) = nnz(Index{i}(j,:));
end
end
  3 件のコメント
David Hill
David Hill 2021 年 1 月 11 日
Works fine for me.
%generates 1x29 Index cell array
for k=1:29
Index{k}=randi(10,29,6)-1;
end
Non_ceros=zeros(length(Index));
for i = 1 : length(Index)
for j = 1 : length(Index)
Non_ceros(i,j) = nnz(Index{i}(j,:));
end
end
%Non_ceros is a 29x29 matrix
Yro
Yro 2021 年 1 月 12 日
I found an error in the other part of my code that generated the array,
Thanks for your time.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by