extracting unique numbers from a cell array
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 1 Mar 2023
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.
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!