I am wondering how can one find unique rows of a cell array when considering only the second and sixth columns of it.
I tried this already: a = unique(strcat(a(:,2), a(:, 6)),'rows')
, where a
is the cell array, but it gives an error - index exceeds matrix dimensions)
.
I tried to use the functions unique
and strcat
because they work for cell type variables with both strings and numbers .
Assuming a
to be the cell array and that you are looking to find unique rows off it based on the column 2
and 6
cells. For the same, one approach based on few unique
usages could be suggested, as shown next -
[~,~,ind1] = unique(cellfun(@num2str,a(:,2),'uni',0)) %//unique indices for col 2
[~,~,ind2] = unique(cellfun(@num2str,a(:,6),'uni',0)) %//unique indices for col 6
[~,ind]= unique([ind1 ind2],'rows','stable')%//unique row indices based on col2,6
a = a(ind,:)
Another approach to shave off one unique
-
col26 = cellfun(@num2str,[a(:,2) a(:,6)],'uni',0)
[~,~,unqind26] = unique(col26(:))
col26indexed = reshape(unqind26,size(col26))
[~,ind] = unique(col26indexed,'rows','stable')
a = a(ind,:)
Notice the use of num2str
as unique
for cell arrays only works with cells of strings, so they had to be converted to strings
.
Both of these approaches would work with a cell array
of numerals
or strings
or a mix of them
(assuming [0]
and '0'
to be the same), but if you would like to have the "uniqueness" based on their datatypes too, then more codes have to added there and since that specific case is not mentioned in the question, it is not considered here.
Also, I don't think you need to use strcat
here from what I understood reading the text of the question.