Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Unique in rows of a matrix.

Subject: Unique in rows of a matrix.

From: Man Garc

Date: 22 Jun, 2011 10:08:04

Message: 1 of 3

Hi all,

I have this matrix:

c= [17 24 1 8 15;
 23 5 0 0 0;
  4 6 6 0 0;
 10 5 19 6 0;
 11 18 25 2 9];

Any idea about how can I convert to 0 the repeated elements in each row? (For example, the element in position 3, 3).

This does not work: d = unique(c,'rows')

Hint: I do not want to use loops (for, etc) as I want the best performance.

Thanks all.

Subject: Unique in rows of a matrix.

From: Sean de

Date: 22 Jun, 2011 11:16:04

Message: 2 of 3

"Man Garc" wrote in message <itseu4$bob$1@newscl01ah.mathworks.com>...
> Hi all,
>
> I have this matrix:
>
> c= [17 24 1 8 15;
> 23 5 0 0 0;
> 4 6 6 0 0;
> 10 5 19 6 0;
> 11 18 25 2 9];
>
> Any idea about how can I convert to 0 the repeated elements in each row? (For example, the element in position 3, 3).
>
> This does not work: d = unique(c,'rows')
>
> Hint: I do not want to use loops (for, etc) as I want the best performance.
>
> Thanks all.

One way:

[csorted,idx] = sort(c,2);
zidx = logical(diff(csorted,[],2));
csorted(:,2:end) = csorted(:,2:end).*zidx;
idx = bsxfun(@plus,idx',0:size(c,2):numel(c)-1);
c2(idx.') = csorted;
c2 = c2.'

Subject: Unique in rows of a matrix.

From: Man Garc

Date: 22 Jun, 2011 11:43:04

Message: 3 of 3

It worked perfectly. Thanks and best regards.

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <itsitk$la5$1@newscl01ah.mathworks.com>...
> "Man Garc" wrote in message <itseu4$bob$1@newscl01ah.mathworks.com>...
> > Hi all,
> >
> > I have this matrix:
> >
> > c= [17 24 1 8 15;
> > 23 5 0 0 0;
> > 4 6 6 0 0;
> > 10 5 19 6 0;
> > 11 18 25 2 9];
> >
> > Any idea about how can I convert to 0 the repeated elements in each row? (For example, the element in position 3, 3).
> >
> > This does not work: d = unique(c,'rows')
> >
> > Hint: I do not want to use loops (for, etc) as I want the best performance.
> >
> > Thanks all.
>
> One way:
>
> [csorted,idx] = sort(c,2);
> zidx = logical(diff(csorted,[],2));
> csorted(:,2:end) = csorted(:,2:end).*zidx;
> idx = bsxfun(@plus,idx',0:size(c,2):numel(c)-1);
> c2(idx.') = csorted;
> c2 = c2.'

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us