extracting unique numbers from a cell array

16 ビュー (過去 30 日間)
Milos
Milos 2023 年 3 月 1 日
コメント済み: Star Strider 2023 年 3 月 1 日
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.

採用された回答

Star Strider
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)
Len2 = 1×4 logical array
0 0 1 1
Gu = unique([G{Len2}])
Gu = 1×4
2 3 4 5
I am not certain how robust this will be to your larger cell array. It works in the provided example.
.
  2 件のコメント
Milos
Milos 2023 年 3 月 1 日
Thansk this works great! I have yet to test anything with larger values
Star Strider
Star Strider 2023 年 3 月 1 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by