extracting unique numbers from a cell array
16 ビュー (過去 30 日間)
古いコメントを表示
Hi, I am trying to extract the unique values from a cell array whose entries are vectors of unspecified length.
To be more precise I have a 10x10x10 cell arrray, call it G and each cell is empty or contians a vector of some unspecfied length. What I would like to do is obtain the unique list of entries of the vectors that have at least length 2.
For example if
G{1} = [1]
G{2} = [1]
G{3} = [2,3,4]
G{4} = [4,5]
and all the other cells are empty then the output would be [2,3,4,5].
What I have so far is
G = grid(U,d,k); %Grid is a built in function that returns a 10x10x10 cell array
H = find(~cellfun('isempty', G));
h = length(H);
counter = zeros(1,h);
for i = 1:h %find all the cells in G that contian at least two values
if length(G{H(i)}) >=2
counter(1,i) = H(i);
end
end
newcounter = nonzeros(counter); %all array positions in array that have vectors of at least length two
GG = G(newcounter); % collection of cells in G whose vectors are at least two
I'm trying to find a function or an efficent way do this. I have tried concatenating GG and apply unique also I have seem the follwoing suggestion which is to try unique(cellfun(@num2str,G,'uni',0)). This to also fails since I just get GG with all the duplicated cells removed.
0 件のコメント
採用された回答
Star Strider
2023 年 3 月 1 日
One approach —
G{1} = [1];
G{2} = [1];
G{3} = [2,3,4];
G{4} = [4,5];
Len2 = cellfun(@(x)numel(x)>=2, G)
Gu = unique([G{Len2}])
I am not certain how robust this will be to your larger cell array. It works in the provided example.
.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!