Syntax
C=uniquecell(C)
Input Parameter
Output Parameter
C : | | cell list with unique elements |
Examples
unique({'A', 2, 4, 6, 'D', 'A'}) % creates an error
uniquecell({'A', 2, 4, 6, 'D', 'A'}) $ creates the expected result
Copyright 2023-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth
Algorithm (Workflow)
This algorithm is designed to find unique elements in a cell array, similar to the 'unique' function in MATLAB, but specifically for cell arrays. It addresses the limitations of the 'unique' function when dealing with cell arrays in newer MATLAB versions.
Input Parameters
- C: A cell array containing elements of various types (e.g., strings, numbers).
Output Results
- C: A cell array with unique elements, preserving the original order.
- ii: Indices of the unique elements in the original cell array.
Algorithm Steps
- Determine the number of elements in the cell array
C
using numel
.
- Create a logical identity matrix
ie
of size nc x nc
.
- Reverse the order of elements in
C
using C(end:-1:1)
.
- Use nested loops to compare each element with every other element in the reversed cell array:
- For each pair of elements
C{i}
and C{k}
, set ie(i,k)
to true
if they are equal using isequal
.
- Identify unique elements by summing each row of
ie
and checking if the sum equals 1.
- Filter the cell array
C
to include only unique elements using the logical index ii
.
- Find the indices of the unique elements in the original order using
find(flipud(ii))
.
- Reverse the order of the filtered cell array
C
to restore the original order of unique elements.
Algorithm explaination created using ChatGPT on 2025-08-19 00:17. (Please note: No guarantee for the correctness of this explanation)
Last html export of this page out of FM database by TL: 2025-09-21