Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding repeated elements in a cell array

996 views
Skip to first unread message

NouveauIX

unread,
Sep 20, 2009, 2:54:15 PM9/20/09
to
okay, say I have cell array:

1a
2b
3c
4d
5d
6f
6g
8h

Is there some function I can use to determine if there is a letter or number repeated in any element of this array? For example, in this there are 2 d's as well as 2 6's. So I need some way to check if there is a repeat in the first character of any element in this array as well as if there is a repeat in the second character of any element, and then produce a 1x1 logical vector with a 1 if there are no repeats and a 0 if there are any repeats of a letter and/or a number. The array above would return a 0. However:

1a
2b
3c
4d
5e
6f
7g
8h

would return a 1, because there is neither a repeated letter or a repeated number. Theoretically I could see if the number of element 1 equals that in elements 2, 3, 4, etc. and then repeat for the rest of the numbers as well as letters but that would be a ridiculous number of lines of code. :p

dpb

unread,
Sep 20, 2009, 4:33:25 PM9/20/09
to
NouveauIX wrote:
> okay, say I have cell array:
>
> 1a
> 2b
> 3c
> 4d
> 5d
> 6f
> 6g
> 8h
>
> Is there some function I can use to determine if there is a letter or
> number repeated in any element of this array? ...

Basically,

u=unique(char(arry(:)));

will give the components of the array. Then for each if

sum(find(..)) > 1

the answer is there is a duplicate.

Thinking about that may lead to some shortcuts; didn't spend much time...

--

dpb

unread,
Sep 20, 2009, 4:52:33 PM9/20/09
to
dpb wrote:
...

> Thinking about that may lead to some shortcuts; didn't spend much time...

One thing came to mind -- not sure otomh how to string all the
characters in the cell array into -- oh, this'll do it if the cell array
is c:

ch=char(c);ch=ch(:)+0; % convert to numeric vector
flg = all(diff(sort(ch)));
% if no repeated values diff() of sorted vector will have no zeroes

--

NouveauIX

unread,
Sep 20, 2009, 5:17:59 PM9/20/09
to
This works perfectly; I never would have thought of trying it that way. This should help a lot for future problems. Thank you!

NouveauIX

unread,
Sep 20, 2009, 5:11:26 PM9/20/09
to
This only gives each element once, even if there is a repeat. For:

char1 =

2a
3d
4c
1g
5h
8f
6e
7f

then u=unique(char(char1(:))) gave me:

u =

1
2
3
4
5
6
7
8
a
c
d
e
f
g
h

so just using find in u won't tell me whether or not there is more than one use of any of those characters in char1.

dpb

unread,
Sep 20, 2009, 11:55:41 PM9/20/09
to
NouveauIX wrote:
> This only gives each element once, even if there is a repeat. ...
...

> then u=unique(char(char1(:))) gave me:
>
> u =
>
> 1
...

> h
>
> so just using find in u won't tell me whether or not there is more
> than one use of any of those characters in char1.

No, but sum() of find() would, albeit repetitively, which was the reason
I said this way needed further thought...

--

dpb

unread,
Sep 20, 2009, 11:58:06 PM9/20/09
to
NouveauIX wrote:
> This works perfectly; I never would have thought of trying it that
> way. This should help a lot for future problems. Thank you!

The key is to think about what are characteristics of the set you're
looking for. I think this should work well as long as the size of the
data set isn't too large so that sort() isn't a real bottleneck.

--

dpb

unread,
Sep 22, 2009, 8:45:42 PM9/22/09
to
dpb wrote:
...

> ch=char(c);ch=ch(:)+0; % convert to numeric vector
...

This has been bugging me--_KNEW_ had to be way to avoid the temporary... :)

Try

all(diff(sort(horzcat(c{:}))))

NOTA BENE: the {} instead of () to dereference the cell contents for
the concatenation operation.

--

thakran...@gmail.com

unread,
Jul 23, 2014, 5:28:13 AM7/23/14
to
We can find out the duplicate element by using Set, list or trvaersing using loop on array.
Below link can be useful to find out the algorithm to find duplicate or repeated elements in an array in java

<a href="http://newtechnobuzzz.blogspot.in/2014/07/find-out-repeated-or-duplicate-element.html">Find out duplicate or repeated elements in an array in java</a>

dpb

unread,
Jul 23, 2014, 8:54:45 AM7/23/14
to
On 07/23/2014 4:28 AM, thakran...@gmail.com wrote:
> On Monday, 21 September 2009 00:24:15 UTC+5:30, NouveauIX wrote:
>> okay, say I have cell array:
>>
>> 1a
>> 2b
>> 3c
>> 4d
>> 5d
>> 6f
>> 6g
>> 8h
>>
>> Is there some function I can use to determine if there is a letter
>> or number repeated in any element of this array? ...

Previous java-related response elided for brevity...

In Matlab, convert cell array to character then use histc() on the columns.

ch=cell2mat(cell_array); % convert to character array
u1=unique(ch(:,1); u2=unique(ch(:,2); % the entries in each column
isOK=~(max(histc(ch(:,1),u1))>1 | max(histc(ch(:,2),u2))>1);

--

Bruno Luong

unread,
Jul 23, 2014, 9:25:12 AM7/23/14
to

>
> ch=cell2mat(cell_array); % convert to character array
> u1=unique(ch(:,1); u2=unique(ch(:,2); % the entries in each column
> isOK=~(max(histc(ch(:,1),u1))>1 | max(histc(ch(:,2),u2))>1);
>

You can save calling HIST or MAX in the last command, simply check the sizes of the variables instead:

isOK = length(u1) == size(ch,1) && length(u2) == size(ch,1)

Bruno

Bruno Luong

unread,
Jul 23, 2014, 9:41:11 AM7/23/14
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <lqod3o$3r3$1...@newscl01ah.mathworks.com>...
>
> >
> > ch=cell2mat(cell_array); % convert to character array
> > u1=unique(ch(:,1); u2=unique(ch(:,2); % the entries in each column
> > isOK=~(max(histc(ch(:,1),u1))>1 | max(histc(ch(:,2),u2))>1);
> >
>
> You can save calling HIST or MAX in the last command, simply check the sizes of the variables instead:
>

Or just a single-line instruction:

isOK = all(all(diff(sort(cell2mat(cell_array)))))

% Bruno

dpb

unread,
Jul 23, 2014, 10:14:19 AM7/23/14
to
Good point, Bruno; I has just implemented the logic of the process as I
was thinking of it w/o worrying about optimizing it...

--


0 new messages